<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>C++_面向对象反汇编补充 | Element</title><meta name="keywords" content="Assemly,C/C++"><meta name="author" content="FHang"><meta name="copyright" content="FHang"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="C++_面向对象反汇编补充1. 类1.1 类的定义 C++中可以使用 struct，class来定义类  1.2 struct和class区别 struct默认成员权限是public class默认成员权限是private  1.2.1 定义和访问 代码示例： 123456789101112131415161718192021222324252627282930struct Person&amp;#123">
<meta property="og:type" content="article">
<meta property="og:title" content="C++_面向对象反汇编补充">
<meta property="og:url" content="https://fhangh.gitee.io/article/c8cc223c.html">
<meta property="og:site_name" content="Element">
<meta property="og:description" content="C++_面向对象反汇编补充1. 类1.1 类的定义 C++中可以使用 struct，class来定义类  1.2 struct和class区别 struct默认成员权限是public class默认成员权限是private  1.2.1 定义和访问 代码示例： 123456789101112131415161718192021222324252627282930struct Person&amp;#123">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_22.jpg">
<meta property="article:published_time" content="2021-12-05T10:05:15.599Z">
<meta property="article:modified_time" content="2022-03-15T05:03:39.694Z">
<meta property="article:author" content="FHang">
<meta property="article:tag" content="Assemly">
<meta property="article:tag" content="C&#x2F;C++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_22.jpg"><link rel="shortcut icon" href="/fhangh/img/fh.png"><link rel="canonical" href="https://fhangh.gitee.io/article/c8cc223c"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="yandex-verification" content="{&quot;theme_color&quot;:{&quot;enable&quot;:true,&quot;main&quot;:&quot;#49B1F5&quot;,&quot;paginator&quot;:&quot;#00c4b6&quot;,&quot;button_hover&quot;:&quot;#FF7242&quot;,&quot;text_selection&quot;:&quot;#00c4b6&quot;,&quot;link_color&quot;:&quot;#99a9bf&quot;,&quot;meta_color&quot;:&quot;#858585&quot;,&quot;hr_color&quot;:&quot;#A4D8FA&quot;,&quot;code_foreground&quot;:&quot;#F47466&quot;,&quot;code_background&quot;:&quot;rgba(27, 31, 35, .05)&quot;,&quot;toc_color&quot;:&quot;#00c4b6&quot;,&quot;blockquote_padding_color&quot;:&quot;#49b1f5&quot;,&quot;blockquote_background_color&quot;:&quot;#49b1f5&quot;}}"/><link rel="stylesheet" href="/fhangh/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/fhangh/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-right"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-03-15 13:03:39'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><meta name="generator" content="Hexo 5.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="/fhangh/img/fh.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/fhangh/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/fhangh/tags/"><div class="headline">标签</div><div class="length-num">10</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/fhangh/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/fhangh/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/fhangh/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/fhangh/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li><li><a class="site-page" href="/fhangh/Gallery/"><i class="fa-fw fas fa-images"></i><span> Image</span></a></li></ul></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img/postImg_22.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/fhangh/">Element</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/fhangh/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/fhangh/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/fhangh/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/fhangh/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li><li><a class="site-page" href="/fhangh/Gallery/"><i class="fa-fw fas fa-images"></i><span> Image</span></a></li></ul></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">C++_面向对象反汇编补充</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-12-05T10:05:15.599Z" title="发表于 2021-12-05 18:05:15">2021-12-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-03-15T05:03:39.694Z" title="更新于 2022-03-15 13:03:39">2022-03-15</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/fhangh/categories/%E7%BC%96%E7%A8%8B/">编程</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">20k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>93分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="C-面向对象反汇编补充"><a href="#C-面向对象反汇编补充" class="headerlink" title="C++_面向对象反汇编补充"></a>C++_面向对象反汇编补充</h1><h3 id="1-类"><a href="#1-类" class="headerlink" title="1. 类"></a>1. 类</h3><h4 id="1-1-类的定义"><a href="#1-1-类的定义" class="headerlink" title="1.1 类的定义"></a>1.1 类的定义</h4><ul>
<li>C++中可以使用 <code>struct</code>，<code>class</code>来定义类</li>
</ul>
<h4 id="1-2-struct和class区别"><a href="#1-2-struct和class区别" class="headerlink" title="1.2 struct和class区别"></a>1.2 struct和class区别</h4><ul>
<li><code>struct</code>默认成员权限是<code>public</code></li>
<li><code>class</code>默认成员权限是<code>private</code></li>
</ul>
<h5 id="1-2-1-定义和访问"><a href="#1-2-1-定义和访问" class="headerlink" title="1.2.1 定义和访问"></a>1.2.1 定义和访问</h5><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_Age;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;m_Age: &quot;</span> &lt;&lt; m_Age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_Age;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;m_Age: &quot;</span> &lt;&lt; m_Age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Person person;</span><br><span class="line"><span class="comment">// 通过对象访问</span></span><br><span class="line">person.m_Age = <span class="number">20</span>;</span><br><span class="line">person.run();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过指针访问</span></span><br><span class="line">Person *ptr_Person = &amp;person;</span><br><span class="line">ptr_Person -&gt;m_Age = <span class="number">20</span>;</span><br><span class="line">ptr_Person -&gt;run();</span><br></pre></td></tr></table></figure></li>
<li><p><code>对象person</code> 和 <code>指针ptr_Person</code>的内存都是在函数的栈空间中，自动分配和回收</p>
</li>
<li><p><code>对象person</code>只有一个<code>int类型</code>的成员变量，所以是<code>4字节</code></p>
</li>
<li><p><code>指针ptr_Person</code>在 <code>32位</code>占用<code>4字节</code>，<code>64位</code>占用<code>8字节</code></p>
</li>
</ul>
<h5 id="1-2-2-反汇编查看区别"><a href="#1-2-2-反汇编查看区别" class="headerlink" title="1.2.2 反汇编查看区别"></a>1.2.2 反汇编查看区别</h5><ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::run() &quot;</span> &lt;&lt; m_price &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car1;</span><br><span class="line">    car1.m_price = <span class="number">10</span>;</span><br><span class="line">    car1.run();</span><br><span class="line">    </span><br><span class="line">    Car car2;</span><br><span class="line">    car2.m_price = <span class="number">20</span>;</span><br><span class="line">    car2.run();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 查看核心部分汇编代码</span><br><span class="line"></span><br><span class="line">mov dword ptr[car1], 0Ah &#x2F;&#x2F; 由此可得，对象占用4字节，成员变量占用4字节，所以第一个成员变量的内存地址就是对象的地址</span><br><span class="line">&#x2F;&#x2F; Car car1;</span><br><span class="line">&#x2F;&#x2F; car1.m_price &#x3D; 10;</span><br><span class="line"></span><br><span class="line">lea ecx, [car1]</span><br><span class="line">call 0086141A &#x2F;&#x2F; 此处 call run()</span><br><span class="line">&#x2F;&#x2F; car1.run();</span><br><span class="line">    </span><br><span class="line">mov dword ptr[car2], 14h</span><br><span class="line">&#x2F;&#x2F; Car car2;</span><br><span class="line"></span><br><span class="line">lea ecx, [car2]</span><br><span class="line">call 0086141A &#x2F;&#x2F; 此处 call run()</span><br><span class="line">&#x2F;&#x2F; car2.m_price &#x3D; 20;</span><br><span class="line">&#x2F;&#x2F; car2.run();</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 由上可以等到，两个对象 car1 和 car2 的成员变量是不同的</span><br><span class="line">&#x2F;&#x2F; 但它们调用的函数的地址是同一个</span><br></pre></td></tr></table></figure></li>
<li><p>对象内的函数不占用对象的内存大小：</p>
<ul>
<li>类中的函数在编译阶段，会将函数的地址存放到单独的一段内存中</li>
<li>当两个函数完全相同时，会去重，仅保留一个函数地址</li>
<li>一个类生成的多个对象中，只有成员变量是每个对象各自拥有的，而成员方法则是统一去重存储在<code>方法列表</code>中</li>
</ul>
</li>
<li><p>补充：</p>
<ul>
<li>无论函数和变量存在于什么地方，如果没有在其它地方使用或调用</li>
<li>该函数和变量，在编译阶段会被优化，也就是根本不存在</li>
</ul>
</li>
</ul>
<h4 id="1-3-对象的内存布局"><a href="#1-3-对象的内存布局" class="headerlink" title="1.3 对象的内存布局"></a>1.3 对象的内存布局</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">int</span> m_age;</span><br><span class="line">  <span class="keyword">int</span> m_id;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    person.m_age = <span class="number">10</span>;</span><br><span class="line">    person.m_id = <span class="number">100</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;&amp;person = &quot;</span> &lt;&lt; &amp;person &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;&amp;person.m_age = &quot;</span> &lt;&lt; &amp;person.m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;&amp;person.m_id = &quot;</span> &lt;&lt; &amp;person.m_id &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>运行结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&amp;person = <span class="number">0</span>x7bfe18</span><br><span class="line">&amp;person.m_age = <span class="number">0</span>x7bfe18   </span><br><span class="line">&amp;person.m_id = <span class="number">0</span>x7bfe1c  </span><br></pre></td></tr></table></figure>







</li>
</ul>
<h3 id="2-this"><a href="#2-this" class="headerlink" title="2. this"></a>2. this</h3><ul>
<li>隐式参数</li>
<li>存储着函数调用者的地址</li>
</ul>
<ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">int</span> m_age;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">    <span class="comment">// 编译器会隐式传参</span></span><br><span class="line">    <span class="comment">// this = &amp;person1</span></span><br><span class="line">      <span class="keyword">this</span>-&gt;m_age = <span class="number">3</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person1;</span><br><span class="line">    person1.m_age = <span class="number">10</span>;</span><br><span class="line">    person1.run();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; ebp-8 是 this 的地址</span><br><span class="line">&#x2F;&#x2F; eax 是 person1 的地址</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; Person person1;</span><br><span class="line">&#x2F;&#x2F; person1.m_age &#x3D; 10;</span><br><span class="line">mov dword ptr [ebp-0Ch], 0Ah</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person.run();</span><br><span class="line">lea ecx, [ebp-0Ch]</span><br><span class="line">call 00181366</span><br><span class="line">	mov dword ptr [ebp-8], ecx</span><br><span class="line">	</span><br><span class="line">	&#x2F;&#x2F; this-&gt;m_age &#x3D; 3;</span><br><span class="line">	mov eax. dword ptr [ebp-8]</span><br><span class="line">	mov dword ptr [eax], 3</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">int</span> m_age;</span><br><span class="line">  <span class="keyword">int</span> m_id;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person1;</span><br><span class="line">    person1.m_age = <span class="number">10</span>;</span><br><span class="line">    person1.m_id = <span class="number">20</span>;</span><br><span class="line">    person1.run();</span><br><span class="line">	</span><br><span class="line">    Person *person2 = &amp;person1;</span><br><span class="line">    person2-&gt;m_age = <span class="number">10</span>;</span><br><span class="line">    person2-&gt;m_id = <span class="number">10</span>;</span><br><span class="line">    person2-&gt;run();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Person person1;</span><br><span class="line">&#x2F;&#x2F; person1.m_age &#x3D; 10;</span><br><span class="line">mov dword ptr [ebp-14h], 0Ah</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person1.m_id &#x3D; 20;</span><br><span class="line">mov dword ptr [ebp-10h], 14h</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person1.run();</span><br><span class="line">lea ecx, [ebp-14h]</span><br><span class="line">call 00FA141F</span><br><span class="line">	</span><br><span class="line">&#x2F;&#x2F; Person *person2 &#x3D; &amp;person1;</span><br><span class="line">lea eax, [ebp-14h] &#x2F;&#x2F; [ebp-14h]是person1的地址</span><br><span class="line">mov dword ptr [ebp-20h], eax &#x2F;&#x2F; [ebp-20h]是指针变量person2的地址</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person2-&gt;m_age &#x3D; 10;</span><br><span class="line">mov eax, dword ptr [ebp-20h] &#x2F;&#x2F; eax存储的是person1的地址值</span><br><span class="line">mov dword ptr [eax], 0Ah </span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person2-&gt;m_id &#x3D; 10;</span><br><span class="line">mov eax, dword ptr [ebp-20h]</span><br><span class="line">mov dword ptr [eax+4], 0Ah </span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; person2-&gt;run();</span><br><span class="line">mov ecx, dword ptr [ebp-20h] &#x2F;&#x2F; 将person1地址隐式传给this</span><br><span class="line">call 003B141F</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例3：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="keyword">int</span> m_age;</span><br><span class="line">  <span class="keyword">int</span> m_id;</span><br><span class="line">  <span class="keyword">int</span> m_height;  </span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">      <span class="built_in">cout</span> &lt;&lt; m_age &lt;&lt; <span class="string">&quot;+&quot;</span> &lt;&lt; m_id &lt;&lt; <span class="string">&quot;+&quot;</span> &lt;&lt; m_height &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    person.m_age = <span class="number">10</span>;</span><br><span class="line">    person.m_id = <span class="number">20</span>;</span><br><span class="line">    person.m_height = <span class="number">30</span>;</span><br><span class="line">	</span><br><span class="line">    Person *p = (Person *) &amp;person.m_age;</span><br><span class="line">    p-&gt;m_age = <span class="number">0</span>;</span><br><span class="line">    p-&gt;m_id = <span class="number">0</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 注意：这里用的 person，不是 p</span></span><br><span class="line">    person.run();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 打印结果：</span><br><span class="line"># <span class="number">10</span>+<span class="number">0</span>+<span class="number">0</span></span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Person *p &#x3D; (Person *) &amp;person.m_id;</span><br><span class="line">&#x2F;&#x2F; eax &#x3D;&#x3D; &amp;person.m_id &#x3D;&#x3D; person+4</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; p-&gt;m_age &#x3D; 0;</span><br><span class="line">mov eax, dword ptr [p]</span><br><span class="line">mov dword ptr [eax + 0], 0h</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; p-&gt;m_id &#x3D; 0;</span><br><span class="line">mov eax, dword ptr [p]</span><br><span class="line">mov dword ptr [eax + 4], 0h</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 在栈空间中&gt;&gt; &amp;person &#x3D;&#x3D; &amp;person.m_age</span><br><span class="line">&#x2F;&#x2F; 改程序中 eax 的存储的地址是 &amp;person.m_id &#x3D;&#x3D; &amp;person+4</span><br><span class="line">&#x2F;&#x2F; 所以&gt;&gt; </span><br><span class="line">&#x2F;&#x2F; p-&gt;m_age &#x3D; 0; 修改的是 m_id</span><br><span class="line">&#x2F;&#x2F; p-&gt;m_id &#x3D; 0; 修改的是 m_height</span><br></pre></td></tr></table></figure></li>
<li><p>思考：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Person *p = (Person *) &amp;person.m_age;</span><br><span class="line">p-&gt;m_age = <span class="number">0</span>;</span><br><span class="line">p-&gt;m_id = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// person 对象的地址传递给 run 的this</span></span><br><span class="line">person.run();</span><br><span class="line"><span class="comment">// 改为</span></span><br><span class="line"><span class="comment">// 将指针 p 里存储的地址值传给 run 的this == &amp;person.m_id 传递给 this</span></span><br><span class="line">p-&gt;run();</span><br><span class="line"></span><br><span class="line"># 打印结果</span><br><span class="line"># <span class="number">40</span>+<span class="number">50</span>+<span class="number">-858993460</span></span><br><span class="line"># <span class="number">-858993460</span> == Oxcccccccc</span><br></pre></td></tr></table></figure></li>
<li><p>补充：</p>
</li>
<li><p>上面的程序打印结果：<code>Oxcccccccc</code></p>
</li>
<li><p>由来：函数调用是，开辟栈空间，调用结束，用<code>cccccccc</code>来填充内存空间</p>
</li>
<li><p>原因：</p>
<ul>
<li><code>cc</code>的汇编是<code>int3</code>：断点</li>
<li><code>int</code>是<code>硬件中断</code></li>
</ul>
</li>
<li><p>作用：防止<code>指针</code>或<code>jump</code>之类的指令，指向错误的内存地址，可能造成系统安全问题，所以用硬件中断指令填充被释放的栈空间</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; p-&gt;run();</span><br><span class="line">&#x2F;&#x2F; 汇编中有一条指令</span><br><span class="line">mov eax, 0cccccccch</span><br><span class="line">rep stos dword ptr es:[edi]</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h3 id="3-内存空间的布局"><a href="#3-内存空间的布局" class="headerlink" title="3. 内存空间的布局"></a>3. 内存空间的布局</h3><ul>
<li>每个应用都有自己独立的内存空间：<ul>
<li>代码区(代码段)：用于存放代码</li>
<li>全局区(数据段)：用于存放全局变量等</li>
<li>栈空间：<ul>
<li>每调用一个函数就会给它分配一个连续的栈空间，等函数调用完毕后会自动回收这段栈空间</li>
<li>自动分配和回收</li>
</ul>
</li>
<li>堆空间：需要主动去申请和释放</li>
</ul>
</li>
</ul>
<h4 id="3-1-代码区"><a href="#3-1-代码区" class="headerlink" title="3.1 代码区"></a>3.1 代码区</h4><ul>
<li>只读</li>
<li>存放CPU指令(机器码)</li>
</ul>
<h4 id="3-2-全局区"><a href="#3-2-全局区" class="headerlink" title="3.2 全局区"></a>3.2 全局区</h4><ul>
<li>程序结束，全局区的数据才会清空</li>
</ul>
<h4 id="3-3-堆空间"><a href="#3-3-堆空间" class="headerlink" title="3.3 堆空间"></a>3.3 堆空间</h4><ul>
<li>在程序运行过程中，为了能自由控制内存的生命周期，大小，会经常使用堆空间的内存</li>
</ul>
<h5 id="3-3-1-堆空间申请和释放"><a href="#3-3-1-堆空间申请和释放" class="headerlink" title="3.3.1 堆空间申请和释放"></a>3.3.1 堆空间申请和释放</h5><ul>
<li><p>堆空间的申请和释放：(<code>C/C++语言 =&gt; malloc() / free()</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">demo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// malloc(4) 申请4字节堆空间，返回 void *，强转成 int *</span></span><br><span class="line">    <span class="keyword">int</span> *p = (<span class="keyword">int</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line">    <span class="comment">// int * 占用4个字节，所以 10 赋值给4个字节空间</span></span><br><span class="line">    *p = <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 释放 p 申请的所有空间</span></span><br><span class="line">    <span class="built_in">free</span>(p);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">char</span> *p = (<span class="keyword">char</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line">    <span class="comment">// char * 占用1个字节空间，所以 10 赋值给 p地址的第一个字节，另外3个字节是空的</span></span><br><span class="line">    *p = <span class="number">10</span>;</span><br><span class="line">    *(p + <span class="number">1</span>) = <span class="number">11</span>;</span><br><span class="line">    *(p + <span class="number">2</span>) = <span class="number">12</span>;</span><br><span class="line">    *(p + <span class="number">3</span>) = <span class="number">13</span>;</span><br><span class="line">    <span class="comment">// 等价于</span></span><br><span class="line">    p[<span class="number">0</span>] = <span class="number">10</span>;</span><br><span class="line">    p[<span class="number">1</span>] = <span class="number">11</span>;</span><br><span class="line">    p[<span class="number">2</span>] = <span class="number">12</span>;</span><br><span class="line">    p[<span class="number">3</span>] = <span class="number">13</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">free</span>(p);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>分析上面代码的内存分布：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> *p = (<span class="keyword">int</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line">*p = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// X86 -- 32bit ，int 指针占用 4 字节，64bit 占用 8字节</span></span><br><span class="line"><span class="comment">// 栈空间</span></span><br><span class="line"># 地址：<span class="number">0x111</span></span><br><span class="line"># 占用：<span class="number">4</span>字节</span><br><span class="line"><span class="meta"># p = 0x999</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 堆空间</span></span><br><span class="line"># 地址：<span class="number">0x999</span></span><br><span class="line"># 占用：<span class="number">4</span>字节</span><br><span class="line"># 值：<span class="number">10</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>堆空间的申请和释放：(<code>C++语言-新 =&gt; new / delete</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">demo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>; <span class="comment">// 等价于  int *p = (int *)malloc(4);</span></span><br><span class="line">    *p = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 释放</span></span><br><span class="line">    <span class="keyword">delete</span> p;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>(<code>new[] / delete []</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">demo</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> *p = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">4</span>]; <span class="comment">// 等价于  char *p = (char *)malloc(4);</span></span><br><span class="line">    *p = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 释放</span></span><br><span class="line">    <span class="keyword">delete</span> p; <span class="comment">// 错误：此时 只会 释放地址p的第一个字节空间，后面3个字节没有释放</span></span><br><span class="line">    <span class="keyword">delete</span>[] p; <span class="comment">// 正确：释放 p 申请的所有空间</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li>注意：<ul>
<li>申请堆空间成功后，会返回那一段空间的地址</li>
<li>申请和释放必须是一一对应的，不然会造成内存泄露</li>
</ul>
</li>
</ul>
<h5 id="3-3-2-堆空间初始化"><a href="#3-3-2-堆空间初始化" class="headerlink" title="3.3.2 堆空间初始化"></a>3.3.2 堆空间初始化</h5><ul>
<li><p>简单示例：(<code>memset == memory set</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> *p1 = (<span class="keyword">int</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">int</span>)); <span class="comment">// *p1 未初始化</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> *p2 = (<span class="keyword">int</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">int</span>));</span><br><span class="line"><span class="built_in">memset</span>(p2, <span class="number">0</span>, <span class="keyword">sizeof</span>(<span class="keyword">int</span>)); <span class="comment">// *p2 的每个字节初始化为0</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>其它初始化方式：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>; <span class="comment">// 未初始化</span></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>(); <span class="comment">// 每个字节初始化为0</span></span><br><span class="line"><span class="comment">// mov __memset(0), eax</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>(<span class="number">5</span>); <span class="comment">// 每个字节初始化为5</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>]; <span class="comment">// 数组三个元素未初始化</span></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>](); <span class="comment">// 数组三个元素初始化为0</span></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>]&#123;&#125;; <span class="comment">// 数组三个元素初始化为0</span></span><br><span class="line"><span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>]&#123;<span class="number">5</span>&#125;; <span class="comment">// 数组首元素初始化为5，其它元素初始化为0</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h6 id="3-3-2-1-memset"><a href="#3-3-2-1-memset" class="headerlink" title="3.3.2.1 memset"></a>3.3.2.1 memset</h6><ul>
<li>memset 函数将较大的数据结构(<code>对象，数组等等</code>)内存清零的比较快的方式</li>
</ul>
<ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Person person;</span><br><span class="line">person.id = <span class="number">1</span>;</span><br><span class="line">person.age = <span class="number">2</span>;</span><br><span class="line">person.height = <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">memset</span>(&amp;person, <span class="number">0</span>, <span class="keyword">sizeof</span>(person));</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Person persons[] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;&#125;;</span><br><span class="line"><span class="built_in">memset</span>(persons, <span class="number">0</span>, <span class="keyword">sizeof</span>(persons));</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="3-4-对象的内存"><a href="#3-4-对象的内存" class="headerlink" title="3.4 对象的内存"></a>3.4 对象的内存</h4><ul>
<li>对象可以存在于3个地方<ol>
<li>全局区(数据段)：全局变量</li>
<li>栈空间：函数里面的局部变量</li>
<li>堆空间：动态申请内存(malloc, new等)</li>
</ol>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 全局区</span></span><br><span class="line">Person person;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 栈空间</span></span><br><span class="line">    Person person;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 堆空间</span></span><br><span class="line">    Person *person = <span class="keyword">new</span> Person;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="4-构造函数"><a href="#4-构造函数" class="headerlink" title="4. 构造函数"></a>4. 构造函数</h3><ul>
<li>类的默认构造函数，无论其对象在什么地方创建，都会调用构造函数</li>
<li>但存在特殊情况，在堆中创建对象，构造函数不会调用</li>
<li>通过<code>malloc()</code>，不会调用构造函数</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Person *person = (Person *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(Person));</span><br><span class="line"><span class="built_in">free</span>(person);</span><br><span class="line"><span class="comment">// malloc 不会调用构造函数</span></span><br><span class="line"></span><br><span class="line">Person *person = <span class="keyword">new</span> Person;</span><br><span class="line"><span class="keyword">delete</span> person;</span><br><span class="line"><span class="comment">// 这个 new 会调用构造函数</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<h4 id="4-1-构造函数的错误概念"><a href="#4-1-构造函数的错误概念" class="headerlink" title="4.1 构造函数的错误概念"></a>4.1 构造函数的错误概念</h4><ul>
<li>错误概念：当类内<code>没有自己定义</code>构造函数，编译器会默认添加一个<code>空实现的，无参的，默认构造函数</code></li>
<li>正确理解：在特定情况下，编译器才会为类生成<code>空的无参构造函数</code></li>
<li>具体情况：后面的<code>虚函数</code>会总结</li>
</ul>
<ul>
<li><p>代码示例：(<code>定义了构造函数</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    </span><br><span class="line">    Person()&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># Person person;</span><br><span class="line">lea ecx, [person]</span><br><span class="line">call Person::Person (0A914BFh)</span><br><span class="line">&#x2F;&#x2F; 定义了构造函数时，对象创建会调用构造函数</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例：(<code>没定义构造函数</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    person.m_age = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># Person person;</span><br><span class="line"># person.m_age &#x3D; 10;</span><br><span class="line">mov dword ptr[person], 0Ah</span><br><span class="line">&#x2F;&#x2F; 类没有定义构造函数时，编译时也不会创建所谓的：空实现的，无参的，默认构造函数</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例：(<code>没定义构造函数，但类中静态初始化了成员变量</code>)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_age = <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    person.m_age = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># Person person;</span><br><span class="line">lea ecx, [person]</span><br><span class="line">call Person::Person (0314C4h)</span><br><span class="line"># person.m_age &#x3D; 10;</span><br><span class="line">mov dword ptr[person], 0Ah</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 没定义构造函数，但类中静态初始化了成员变量，此时编译器会创建：空实现的，无参的，默认构造函数</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="4-2-构造函数的调用"><a href="#4-2-构造函数的调用" class="headerlink" title="4.2 构造函数的调用"></a>4.2 构造函数的调用</h4><ul>
<li>通过多种创建对象的方式，判读是否调用了类的构造函数</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        m_age = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Person(<span class="keyword">int</span> age)</span><br><span class="line">    &#123;</span><br><span class="line">        m_age = age;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person(int)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Person g_person0; <span class="comment">// Person()</span></span><br><span class="line"><span class="function">Person <span class="title">g_person1</span><span class="params">()</span></span>; <span class="comment">// 这是 g_person1() 函数的声明或定义，不会调用构造函数</span></span><br><span class="line"><span class="function">Person <span class="title">g_person2</span><span class="params">(<span class="number">10</span>)</span></span>; <span class="comment">// Person(int)</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person0; <span class="comment">// Person()</span></span><br><span class="line">    <span class="function">Person <span class="title">person1</span><span class="params">()</span></span>; <span class="comment">// 这是 g_person1() 函数的声明或定义，不会调用构造函数</span></span><br><span class="line">    <span class="function">Person <span class="title">person2</span><span class="params">(<span class="number">20</span>)</span></span>; <span class="comment">// Person(int)</span></span><br><span class="line"></span><br><span class="line">    Person *p0 = <span class="keyword">new</span> Person; <span class="comment">// Person()</span></span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person(); <span class="comment">// Person() ，这是在堆空间中创建对象</span></span><br><span class="line">    Person *p2 = <span class="keyword">new</span> Person(<span class="number">30</span>); <span class="comment">// Person(int)</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo1</span><br><span class="line">Person()</span><br><span class="line">Person(int)</span><br><span class="line">Person()</span><br><span class="line">Person(int)</span><br><span class="line">Person()</span><br><span class="line">Person()</span><br><span class="line">Person(int)</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="4-3-构造函数的成员变量初始化"><a href="#4-3-构造函数的成员变量初始化" class="headerlink" title="4.3 构造函数的成员变量初始化"></a>4.3 构造函数的成员变量初始化</h4><ul>
<li><p>默认情况下，没有自定义构造函数时，只有全局区和堆区的对象创建，会初始化为0，其余栈空间不会初始化成员变量</p>
</li>
<li><p>如果自定义了构造函数，除了全局区，其它内存空间的成员变量默认都不会被初始化，需要手动初始化</p>
</li>
</ul>
<ul>
<li><p>代码示例1：<code>(无自定义构造函数)</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 堆空间中的，无论是对象内的成员变量，还是普通的数据类型，都会默认初始化为 0</span></span><br><span class="line">Person g_person;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 栈空间的对象直接被编译器优化了，不会初始化成员变量</span></span><br><span class="line">    <span class="comment">// Person person;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 没有初始化</span></span><br><span class="line">    Person *p0 = <span class="keyword">new</span> Person;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化</span></span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; g_person.m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p0-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p1-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo2</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">14184048</span></span><br><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure></li>
<li><p>得出结论：</p>
<ol>
<li><code>全局区</code>内，内存值默认全是 <code>0</code></li>
<li><code>堆空间</code>内，通过 <code>()</code> 申请的内存，默认初始化为 <code>0</code></li>
<li><code>栈空间</code>内，默认初始化为 <code>cccccccc</code></li>
</ol>
</li>
</ul>
<ul>
<li><p>代码示例2：<code>(有自定义构造函数) -&gt; 空实现</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    </span><br><span class="line">    Person()&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 堆空间中的，无论是对象内的成员变量，还是普通的数据类型，都会默认初始化为 0</span></span><br><span class="line">Person g_person;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 没有初始化</span></span><br><span class="line">    Person *p0 = <span class="keyword">new</span> Person;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 没有初始化，初始化工作交给自定义构造函数，然而自定义构造函数是空实现，所以没有初始化</span></span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; g_person.m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p0-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p1-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo2</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">14184048</span></span><br><span class="line"><span class="number">14184048</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="4-4-构造函数的集体初始化"><a href="#4-4-构造函数的集体初始化" class="headerlink" title="4.4 构造函数的集体初始化"></a>4.4 构造函数的集体初始化</h4><ul>
<li>通过<code>memset()</code>方法实现</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// memset() 方法，方便用于类成员变量集体初始化或清零</span></span><br><span class="line">        <span class="built_in">memset</span>(<span class="keyword">this</span>, <span class="number">0</span>, <span class="keyword">sizeof</span>(Person));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p1-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo3</span><br><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h3 id="5-析构函数"><a href="#5-析构函数" class="headerlink" title="5. 析构函数"></a>5. 析构函数</h3><ul>
<li>又名<code>析构器</code>，在对象销毁时自动调用，一般用于完成对象的清理工作</li>
<li>没有返回值，不能重载</li>
<li>通过<code>malloc()</code>分配的对象，<code>free()</code>时，不会调用析构函数</li>
<li>类中的构造函数和析构函数，需要<code>public:</code>修饰后，才能被外界调用</li>
<li>全局区的对象，不会调用析构函数</li>
</ul>
<h4 id="5-1-malloc与析构函数"><a href="#5-1-malloc与析构函数" class="headerlink" title="5.1 malloc与析构函数"></a>5.1 malloc与析构函数</h4><ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ~Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// malloc 的 对象不会调用 构造函数 和 析构函数</span></span><br><span class="line">    Person *p0 = (Person *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(Person));</span><br><span class="line">    <span class="built_in">free</span>(p0);</span><br><span class="line"></span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person();</span><br><span class="line">    <span class="keyword">delete</span> p1;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo4</span><br><span class="line">Person()</span><br><span class="line">~Person()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="5-2-对象内申请的堆区对象"><a href="#5-2-对象内申请的堆区对象" class="headerlink" title="5.2 对象内申请的堆区对象"></a>5.2 对象内申请的堆区对象</h4><ul>
<li>对象析构时，只会回收对象，如果对象的成员变量指向堆空间，需要手动释放，否则会造成<code>内存泄漏</code></li>
<li>内存泄漏：该释放的内存，没有去释放</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line"></span><br><span class="line">    Car()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;   </span><br><span class="line">    ~Car()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;~Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    Car *m_car;</span><br><span class="line"></span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 此处的 m_car是在堆空间中声明，需要delete才能回收</span></span><br><span class="line">        <span class="comment">// 如果是栈空间声明，person在调用析构函数时，会先回收m_car</span></span><br><span class="line">        m_car = <span class="keyword">new</span> Car;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ~Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">delete</span> m_car;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\demo&gt; .\demo5</span><br><span class="line">Car()</span><br><span class="line">Person()</span><br><span class="line">~Car()</span><br><span class="line">~Person()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="6-声明和实现"><a href="#6-声明和实现" class="headerlink" title="6. 声明和实现"></a>6. 声明和实现</h3><ul>
<li>类的定义和声明分离，分别在 <code>.h</code>和<code>.cpp</code>中实现</li>
</ul>
<h4 id="6-1-具体演示"><a href="#6-1-具体演示" class="headerlink" title="6.1 具体演示"></a>6.1 具体演示</h4><p><code>Person.h</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Person();</span><br><span class="line">    ~Person();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">set_Age</span><span class="params">(<span class="keyword">int</span> age)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">get_Age</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<p><code>Person.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Person::Person()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line">Person::~Person()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Person::set_Age</span><span class="params">(<span class="keyword">int</span> age)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    m_age = age;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">Person::get_Age</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> m_age;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p><code>main.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    person.set_Age(<span class="number">10</span>);</span><br><span class="line">    <span class="keyword">int</span> age = person.get_Age();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<ul>
<li><p>打印结果：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PS C:\Users\Admin\Desktop\Project\Demo2&gt; .\demo1.exe</span><br><span class="line">Person()</span><br><span class="line"><span class="number">10</span></span><br><span class="line">~Person()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="7-命名空间"><a href="#7-命名空间" class="headerlink" title="7. 命名空间"></a>7. 命名空间</h3><ul>
<li>作用：<ul>
<li>解决重复命名的问题</li>
<li>命名空间不影响内存布局和结构</li>
</ul>
</li>
</ul>
<h4 id="7-1-命名空间的基本用法"><a href="#7-1-命名空间的基本用法" class="headerlink" title="7.1 命名空间的基本用法"></a>7.1 命名空间的基本用法</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> fh1</span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">    <span class="keyword">private</span>:</span><br><span class="line">        <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">        Person();</span><br><span class="line">        ~Person();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> fh2</span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">    <span class="keyword">private</span>:</span><br><span class="line">        <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">        Person();</span><br><span class="line">        ~Person();</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    fh1::Person person1;</span><br><span class="line">    fh2::Person person2;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="7-2-命名空间的嵌套"><a href="#7-2-命名空间的嵌套" class="headerlink" title="7.2 命名空间的嵌套"></a>7.2 命名空间的嵌套</h4><ul>
<li>命名空间可以一直嵌套</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> fh1</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">namespace</span> fh2</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">        &#123;</span></span><br><span class="line">        <span class="keyword">private</span>:</span><br><span class="line">            <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span>:</span><br><span class="line">            Person();</span><br><span class="line">            ~Person();</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    fh1::fh2::Person person;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="7-3-存在最大默认命名空间"><a href="#7-3-存在最大默认命名空间" class="headerlink" title="7.3 存在最大默认命名空间"></a>7.3 存在最大默认命名空间</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> fh</span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;fh::func()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    fh::func();</span><br><span class="line">    <span class="comment">// 通过默认的全局命名空间，访问 fh</span></span><br><span class="line">    ::fh::func();</span><br><span class="line"></span><br><span class="line">    func();</span><br><span class="line">    </span><br><span class="line">   	<span class="comment">// 通过默认的全局命名空间，访问 func()</span></span><br><span class="line">    ::func();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo2&gt; .\demo4.exe</span><br><span class="line">fh::func()</span><br><span class="line">fh::func()</span><br><span class="line">func()</span><br><span class="line">func()</span><br></pre></td></tr></table></figure>







</li>
</ul>
<h3 id="8-继承"><a href="#8-继承" class="headerlink" title="8. 继承"></a>8. 继承</h3><ul>
<li>继承可以让子类拥有父类的所有成员</li>
</ul>
<h4 id="8-1-继承的内存布局"><a href="#8-1-继承的内存布局" class="headerlink" title="8.1 继承的内存布局"></a>8.1 继承的内存布局</h4><ul>
<li>子对象中的成员变量分布：最上层的父类的成员变量在子对象的首地址，依次向下排列</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> height;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> age;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">My</span> :</span> <span class="keyword">public</span> Student</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> id;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person;</span><br><span class="line">    Student student;</span><br><span class="line">    My my;</span><br><span class="line">    my.height = <span class="number">9</span>;</span><br><span class="line">    my.age = <span class="number">10</span>;</span><br><span class="line">    my.id = <span class="number">11</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(person) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(student) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;My: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(my) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; &amp;my.height &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; &amp;my.age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; &amp;my.id &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo3&gt; .\demo1.exe</span><br><span class="line">Person: <span class="number">4</span></span><br><span class="line">Student: <span class="number">8</span></span><br><span class="line">My: <span class="number">12</span></span><br><span class="line"><span class="number">0</span>x7bfe08</span><br><span class="line"><span class="number">0</span>x7bfe0c</span><br><span class="line"><span class="number">0</span>x7bfe10</span><br></pre></td></tr></table></figure></li>
<li><p>总结：父类的成员变量在前面，子类的在后面</p>
</li>
<li><p>补充：父类的成员变量，子类中没有使用，子类的对象大小依然不变，编译器不会去优化，所以要合理设计类</p>
</li>
</ul>
<h4 id="8-2-成员访问权限"><a href="#8-2-成员访问权限" class="headerlink" title="8.2 成员访问权限"></a>8.2 成员访问权限</h4><ul>
<li><p>成员访问权限、继承方式有3种：</p>
<ol>
<li><code>public</code>：公共的，任何地方都可以访问（<code>struct默认</code>）</li>
<li><code>private</code>：私有的，只有当前类内部可以访问（<code>class默认</code>）</li>
<li><code>protected</code>：子类内部和当前类可以访问</li>
</ol>
</li>
<li><p>子类在继承父类时，使用了权限修饰：子类内部访问父类成员的权限，是一下两项中权限最小的</p>
<ol>
<li>成员本身的访问权限</li>
<li>上一级父类的继承方式</li>
</ol>
</li>
<li><p>开发中最多的继承方式是<code>public</code>继承，保留父类原来的成员访问权限</p>
</li>
<li><p>访问权限，不影响对象的内存布局</p>
</li>
</ul>
<h3 id="9-初始化列表"><a href="#9-初始化列表" class="headerlink" title="9. 初始化列表"></a>9. 初始化列表</h3><ul>
<li>特点：<ul>
<li>一种便捷的初始化成员变量的方式</li>
<li>只用于构造函数</li>
<li>初始化顺序，只跟成员变量的声明顺序有关</li>
</ul>
</li>
<li>补充：<ul>
<li>如果构造函数的声明和实现分离，初始化列表只能写在实现中</li>
</ul>
</li>
</ul>
<ul>
<li><p>语法：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="keyword">int</span> m_height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 普通写法</span></span><br><span class="line">    Person(<span class="keyword">int</span> age, <span class="keyword">int</span> height)</span><br><span class="line">    &#123;</span><br><span class="line">        m_age = age;</span><br><span class="line">        m_height = height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化列表</span></span><br><span class="line">    Person(<span class="keyword">int</span> age, <span class="keyword">int</span> height) : m_age(age), m_height(height)</span><br><span class="line">    &#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>两种写法完全等价：(反汇编查看)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 普通构造函数</span><br><span class="line">&#x2F;&#x2F; m_age &#x3D; age;</span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov ecx, dword ptr[age]</span><br><span class="line">mov dword ptr[eax], ecx</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; m_height &#x3D; height;</span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov ecx, dword ptr[height]</span><br><span class="line">mov dword ptr [eax+4], ecx</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 初始化列表</span><br><span class="line">&#x2F;&#x2F; m_age(age)</span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov ecx, dword ptr[age]</span><br><span class="line">mov dword ptr[eax], ecx</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; m_height(height)</span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov ecx, dword ptr[height]</span><br><span class="line">mov dword ptr [eax+4], ecx</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="10-构造函数互调"><a href="#10-构造函数互调" class="headerlink" title="10. 构造函数互调"></a>10. 构造函数互调</h3><h4 id="10-1-互调语法"><a href="#10-1-互调语法" class="headerlink" title="10.1 互调语法"></a>10.1 互调语法</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="keyword">int</span> m_height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 错误的写法</span></span><br><span class="line">    <span class="comment">// Person() &#123;Person(10, 20);&#125;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 正确的写法</span></span><br><span class="line">    Person() : Person(<span class="number">0</span>, <span class="number">0</span>)&#123;&#125;</span><br><span class="line"></span><br><span class="line">    Person(<span class="keyword">int</span> age, <span class="keyword">int</span> height)</span><br><span class="line">    &#123;</span><br><span class="line">        m_age = age;</span><br><span class="line">        m_height = height;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; m_age &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_height &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person person1;</span><br><span class="line">    <span class="function">Person <span class="title">person2</span><span class="params">(<span class="number">10</span>, <span class="number">20</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo3&gt; .\demo3.exe</span><br><span class="line"><span class="number">0</span> <span class="number">0</span></span><br><span class="line"><span class="number">10</span> <span class="number">20</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="10-2-语法分析"><a href="#10-2-语法分析" class="headerlink" title="10.2 语法分析"></a>10.2 语法分析</h4><ul>
<li><p>通过反汇编分析两种写法的正确性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 错误的写法</span><br><span class="line">&#x2F;&#x2F; 原因：在函数体中写的 Person(0, 0); 是创建临时Person的对象，0 赋值给了临时的Person对象</span><br><span class="line">&#x2F;&#x2F; Person() &#123;Person(0, 0);&#125;</span><br><span class="line">lea ecx, [person1]</span><br><span class="line">call Person::Person(0E61118h)</span><br><span class="line"></span><br><span class="line">mov dword ptr [this], ecx</span><br><span class="line">push 14h</span><br><span class="line">push 0Ah</span><br><span class="line"></span><br><span class="line">lea ecx, [person] &#x2F;&#x2F; person是临时对象</span><br><span class="line">call Person::Person(0E61311h)</span><br><span class="line"></span><br><span class="line">mov dword ptr[this], ecx</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 正确的写法</span><br><span class="line">&#x2F;&#x2F; Person() : Person(0, 0)&#123;&#125;</span><br><span class="line">lea ecx, [person2]</span><br><span class="line">call Person::Person(01221118h)</span><br><span class="line"></span><br><span class="line">mov dword ptr [this], ecx</span><br><span class="line">push 14h</span><br><span class="line">push 0Ah</span><br><span class="line"></span><br><span class="line">mov dword ptr[this], ecx</span><br><span class="line">call Person::Person(01221311h)</span><br><span class="line"></span><br><span class="line">mov dword ptr[this], ecx</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="10-3-父类的构造函数"><a href="#10-3-父类的构造函数" class="headerlink" title="10.3 父类的构造函数"></a>10.3 父类的构造函数</h4><ul>
<li>子类的构造函数默认会调用父类的无参构造函数</li>
<li>子类的构造函数主动调用了父类的有参构造函数，则父类的无参构造函数不会被调用</li>
<li>如果父类缺少无参构造函数，子类构造函数就必须显式调用父类的有参构造函数</li>
</ul>
<ul>
<li><p>代码示例1：(默认会调用父类的无参构造函数)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Student()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::Student()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Student student;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo3&gt; .\demo4.exe</span><br><span class="line">Person::Person()</span><br><span class="line">Student::Student()</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例2：(主动调用了父类的有参构造函数)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Person(<span class="keyword">int</span> age)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person(int)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Student() : Person(<span class="number">10</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::Student()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Student student;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo3&gt; .\demo4.exe</span><br><span class="line">Person::Person(int)</span><br><span class="line">Student::Student()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="10-4-构造和析构的顺序"><a href="#10-4-构造和析构的顺序" class="headerlink" title="10.4 构造和析构的顺序"></a>10.4 构造和析构的顺序</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ~Person()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    Student()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::Student()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ~Student()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::~Student()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Student student;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo3&gt; .\demo5.exe</span><br><span class="line">Person::Person()</span><br><span class="line">Student::Student()</span><br><span class="line">Student::~Student()</span><br><span class="line">Person::~Person()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="11-多态"><a href="#11-多态" class="headerlink" title="11. 多态"></a>11. 多态</h3><ul>
<li>默认情况下，编译器只会根据指针类型调用对应的函数，不存在多态</li>
</ul>
<ul>
<li>多态是面向对象非常重要的特性<ul>
<li>同一个操作，用于不同的对象，可以有不同的解释，产生不同的结果</li>
<li>在运行时，可以识别出真正的对象类型，调用对应子类中的函数</li>
</ul>
</li>
</ul>
<ul>
<li>多态的要素：<ul>
<li>子类重写父类的成员函数<code>(override)</code></li>
<li>父类指针指向子类</li>
<li>利用父类指针调用重写的成员函数</li>
</ul>
</li>
</ul>
<h4 id="11-1-父类和子类指针"><a href="#11-1-父类和子类指针" class="headerlink" title="11.1 父类和子类指针"></a>11.1 父类和子类指针</h4><ul>
<li>父类指针指向子类对象是安全的，子类的继承方式是<code>public</code></li>
<li>子类指针指向父类对象是不安全的</li>
</ul>
<ul>
<li><p>代码示例1：父类指针指向子类对象</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> :</span> Person</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person *person = <span class="keyword">new</span> Student;</span><br><span class="line">    person-&gt;m_age = <span class="number">10</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;person-&gt;m_age: &quot;</span> &lt;&lt; person-&gt;m_age &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; .\demo1.exe</span><br><span class="line">person-&gt;m_age: <span class="number">10</span></span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>父类指针指向子类对象，这样用父类指针使用成员变量时，会很<code>安全</code>，不会影响到子类独有的成员变量</li>
<li>父类的成员变量存在得到内存空间，肯定在子类对象的内存范围内，不会超出子类对象的内存范围，所以<code>安全</code></li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例2：子类指针指向父类对象</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> :</span> Person</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;    </span><br><span class="line">    Student *student = (Student *)<span class="keyword">new</span> Person;</span><br><span class="line">    student-&gt;m_age = <span class="number">10</span>;</span><br><span class="line">    student-&gt;m_score = <span class="number">100</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;m_age: &quot;</span> &lt;&lt; student-&gt;m_age &lt;&lt; <span class="string">&quot;\n&quot;</span> </span><br><span class="line">         &lt;&lt; <span class="string">&quot;m_score: &quot;</span> &lt;&lt; student-&gt;m_score &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; .\demo1.exe</span><br><span class="line">m_age: <span class="number">10</span></span><br><span class="line">m_score: <span class="number">100</span></span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>子类指针指向父类的对象，子类指针指向的成员变量，可能会超出父类对象内存范围内的成员变量，<code>不安全</code></li>
<li>因为可能会覆盖掉，别的子类对象内存范围内的成员变量的值</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例3：通过反汇编查看强制类型转换</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Cat *animal = (Cat *)<span class="keyword">new</span> Dog;</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    animal-&gt;run();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; .\demo1.exe</span><br><span class="line"><span class="built_in">Cat</span>::speak()</span><br><span class="line"><span class="built_in">Cat</span>::run()</span><br></pre></td></tr></table></figure></li>
<li><p>查看反汇编结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Cat *animal &#x3D; (Cat *)new Dog;</span><br><span class="line">mov dword ptr[ebp-0DCh], 0</span><br><span class="line">mov ecx, dword ptr[ebp-0DCh]</span><br><span class="line">mov dword ptr[animal], ecx</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; animal-&gt;speak();</span><br><span class="line">mov ecx, dword ptr[animal]</span><br><span class="line">call Cat::speak(0F714F6h)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; animal-&gt;run();</span><br><span class="line">mov ecx, dword ptr[animal]</span><br><span class="line">call Cat::run(0F714F1h)</span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>由上面的<code>示例3</code>可知：指针的类型决定了，指针所能访问的内存范围</li>
<li>此时还未实现<code>多态</code></li>
</ul>
</li>
</ul>
<h4 id="11-2-虚函数实现多态"><a href="#11-2-虚函数实现多态" class="headerlink" title="11.2 虚函数实现多态"></a>11.2 虚函数实现多态</h4><ul>
<li>通过虚函数<code>(virtual function)</code>实现多态</li>
<li>虚函数：被<code>virtual</code>修饰的成员函数</li>
<li>在<code>父类</code>中声明为<code>虚函数</code>，子类中<code>重写</code>的成员函数会自动<code>转成虚函数</code></li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">playAnimal</span><span class="params">(Animal *animal)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    animal-&gt;run();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    playAnimal(<span class="keyword">new</span> Dog);</span><br><span class="line">    playAnimal(<span class="keyword">new</span> Cat);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; .\demo2.exe</span><br><span class="line">Dog::speak()</span><br><span class="line">Dog::run()</span><br><span class="line"><span class="built_in">Cat</span>::speak()</span><br><span class="line"><span class="built_in">Cat</span>::run()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="11-3-虚表"><a href="#11-3-虚表" class="headerlink" title="11.3 虚表"></a>11.3 虚表</h4><ul>
<li>虚函数的实现原理是<code>虚表</code>，这个虚表里存储着最终需要调用的<code>虚函数地址</code>，这个虚表也叫虚函数表</li>
</ul>
<h5 id="11-3-1-虚表的大小"><a href="#11-3-1-虚表的大小" class="headerlink" title="11.3.1 虚表的大小"></a>11.3.1 虚表的大小</h5><ul>
<li>前提：<ul>
<li>类中的普通函数，在对象中不占用空间，因为函数存在代码区中，而对象在栈区中，只有成员变量占用空间</li>
<li>父类的虚函数，在子类重写后，这些函数会存入一个虚函数表中，虚函数表占用子类对象的内存空间</li>
<li>x86环境，指针是4字节</li>
<li>x64环境，指针是8字节</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_height;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span></span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_health;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Cat::sizeof()= &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(Cat) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::sizeof()= &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(Dog) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; ./demo3.exe</span><br><span class="line"><span class="built_in">Cat</span>::sizeof()= <span class="number">4</span></span><br><span class="line">Dog::sizeof()= <span class="number">16</span></span><br></pre></td></tr></table></figure></li>
<li><p>结果分析：</p>
<ul>
<li>Dog对象，继承了父类Animal，对象类有<code>两个int类型</code>成员变量：<code>8字节</code></li>
<li>父类还<code>有虚函数</code>的存在，所以需要在Dog对象内<code>创建虚函数表</code>：<code>x86是4字节</code>，<code>x64是8字节</code></li>
<li>Dog对象重写的父类的方法，存在代码区中，不和对象一起在栈区中，所以不占用对象内存</li>
</ul>
</li>
</ul>
<h5 id="11-3-2-反汇编分析虚表"><a href="#11-3-2-反汇编分析虚表" class="headerlink" title="11.3.2 反汇编分析虚表"></a>11.3.2 反汇编分析虚表</h5><ul>
<li><p>x86环境中，内存的布局</p>
</li>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog();</span><br><span class="line">    animal-&gt;m_age = <span class="number">20</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th></th>
<th>内存地址</th>
<th>内存数据</th>
<th>内存地址</th>
<th>内存数据</th>
</tr>
</thead>
<tbody><tr>
<td><code>dog</code></td>
<td><code>0x00E69B60</code></td>
<td><code>0x00B89B64</code>==<code>虚表内存地址</code></td>
<td><code>0x00B89B64</code></td>
<td><code>0x00B814E7</code>==<code>Dog::speak()的调用地址</code></td>
</tr>
<tr>
<td></td>
<td>0x00E69B61</td>
<td></td>
<td>0x00B89B65</td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B62</td>
<td></td>
<td>0x00B89B66</td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B63</td>
<td></td>
<td>0x00B89B67</td>
<td></td>
</tr>
<tr>
<td><code>&amp;m_age</code></td>
<td><code>0x00E69B64</code></td>
<td><code>20</code></td>
<td><code>0x00B89B68</code></td>
<td><code>0x00B814CE</code>==<code>Dog::run()的调用地址</code></td>
</tr>
<tr>
<td></td>
<td>0x00E69B65</td>
<td></td>
<td>0x00B89B69</td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B66</td>
<td></td>
<td>0x00B89B6A</td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B67</td>
<td></td>
<td>0x00B89B6B</td>
<td></td>
</tr>
<tr>
<td><code>&amp;m_height</code></td>
<td><code>0x00E69B68</code></td>
<td><code>0</code></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B69</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B6A</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>0x00E69B6B</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
</li>
<li><p>总结：</p>
<ul>
<li>可以看到，<code>父类</code>中有<code>虚函数</code>，<code>子类对象</code>从<code>首地址开始</code>，<code>存放虚函数表</code></li>
<li><code>虚函数表</code>内<code>存放着内存地址</code>，而这个内存地址<code>指向</code>的是<code>代码区的地址</code>，也正是子类对象内重写父类方法的地址</li>
<li>所以实现多态后，父类指针指向子类对象时，可以<code>通过虚函数表</code>，使得<code>指针调用子类</code>的方法，以达到多态的目的</li>
</ul>
</li>
</ul>
<ul>
<li><p>反汇编查看：</p>
</li>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog();</span><br><span class="line">    animal-&gt;m_age = <span class="number">20</span>;</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; animal-&gt;m_age &#x3D; 20;</span><br><span class="line">mov eax, dword ptr[animal] &#x2F;&#x2F; animal &#x3D;&#x3D; ebp-8 是指针变量，eax 是Dog对象的地址，里面存放着虚表的地址值</span><br><span class="line">mov dword ptr[eax+4], 14h</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; animal-&gt;speak();</span><br><span class="line">mov eax, dword ptr[animal]</span><br><span class="line">mov edx, dword ptr[eax] &#x2F;&#x2F; 从Dog对象的地址开始往后4个字节内的数据存入到 寄存器edx 中，edx 得到 虚表的地址</span><br><span class="line"></span><br><span class="line">mov esi, esp</span><br><span class="line">mov ecx, dword ptr[animal]</span><br><span class="line">mov eax, dword ptr[edx] &#x2F;&#x2F; 从 edx 中取4个字节，eax 得到虚表中存放 Dog对象方法的地址，(但不是方法的真正地址，需要jump到真正地址)</span><br><span class="line"></span><br><span class="line">call eax &#x2F;&#x2F; 调用 Dog对象的方法</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h5 id="11-3-3-虚表的设计思想"><a href="#11-3-3-虚表的设计思想" class="headerlink" title="11.3.3 虚表的设计思想"></a>11.3.3 虚表的设计思想</h5><ul>
<li>前提：项目中实现了多态，其父类指针多次指向不同的子类对象，且父类的方法用<code>virtual修饰</code></li>
<li>原理：<ul>
<li>编译器在<code>编译时</code>，并<code>不知道</code>，项目中父类指针多次指向不同的子类对象</li>
<li>即便<code>父类指针</code>指向的对象是<code>自己本身</code>的对象，也<code>一样生成</code>用于<code>存放虚表地址</code>的<code>内存空间</code></li>
<li><code>编译文件</code>是<code>静态</code>的，而程序在内存中<code>执行</code>是<code>动态</code>过程</li>
</ul>
</li>
<li>目的：<ul>
<li>多态中，每个<code>子对象</code>都要有一个<code>虚函数表</code></li>
<li>实现程序在内存中<code>执行</code>的<code>动态性</code></li>
</ul>
</li>
<li>虚表的相关优化：<ul>
<li>当父类指针多次指向<code>多个</code>子类对象，且都是<code>同一个类</code>的对象</li>
<li>此时，这些对象依然有<code>各自独立</code>存放<code>虚表地址</code>的内存空间</li>
<li>这些子类对象的<code>前4字节</code>空间中存放的<code>虚表地址值</code>(对象的方法所在代码区的地址)是<code>相等</code>的</li>
</ul>
</li>
<li>总结：相同类的对象，不管在什么区中，共用一份虚表</li>
</ul>
<h5 id="11-3-4-多态实现的需求"><a href="#11-3-4-多态实现的需求" class="headerlink" title="11.3.4 多态实现的需求"></a>11.3.4 多态实现的需求</h5><ul>
<li>实现需求：大致两种<ol>
<li>完全重写父类的成员函数</li>
<li>在父类的成员函数基础上，利用多态附加子类对象的成员函数</li>
</ol>
</li>
</ul>
<ul>
<li><p>代码示例1：第一种<code>(完全重写)</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 子类会完全重写的虚函数是 可以 为空实现</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog;</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; ./demo4.exe</span><br><span class="line">Dog::speak()</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例2：第二种<code>(父类的方法实现基础上附加子类的实现)</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 父类的方法实现基础上附加子类的实现，此处的父类虚函数实现 可以 不为空实现</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Animal::speak();</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog;</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; ./demo4.exe</span><br><span class="line">Animal</span><br><span class="line">Dog::speak()</span><br></pre></td></tr></table></figure>







</li>
</ul>
<h4 id="11-4-虚析构函数"><a href="#11-4-虚析构函数" class="headerlink" title="11.4 虚析构函数"></a>11.4 虚析构函数</h4><ul>
<li>父类指针指向子类对象，含有虚函数的父类，应该将析构函数声明为虚函数(虚析构函数)</li>
<li><code>delete</code>父类指针，才会调用子类的析构函数，保证析构的完整型</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Animal()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::Animal()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="keyword">virtual</span> ~Animal()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal::~Animal()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Dog()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::Dog()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    ~Dog()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::~Dog()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog;</span><br><span class="line">    <span class="keyword">delete</span> animal;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo4&gt; ./demo5.exe</span><br><span class="line">Animal::Animal()</span><br><span class="line">Dog::Dog()</span><br><span class="line">Dog::~Dog()</span><br><span class="line">Animal::~Animal()</span><br></pre></td></tr></table></figure></li>
<li><p>补充：</p>
<ul>
<li>先构造父类，再构造子类</li>
<li>先析构子类，再析构父类</li>
<li>父类构造可以不用<code>virtual</code>修饰</li>
</ul>
</li>
</ul>
<h4 id="11-5-纯虚函数"><a href="#11-5-纯虚函数" class="headerlink" title="11.5 纯虚函数"></a>11.5 纯虚函数</h4><ul>
<li><p>定义：没有函数体其初始化为0的虚函数，用来定义接口规范</p>
</li>
<li><p>抽象类（<code>Abstract Class</code>）</p>
<ul>
<li>含有纯虚函数的类，不可以实例化（<code>不可以创建对象</code>）</li>
<li>抽象类也可以是包含非纯虚函数，成员变量</li>
<li>如果父类是抽象类，子类没有完全实现纯虚函数，那么这个子类依然是抽象类</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">speak</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::speak()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Animal *animal = <span class="keyword">new</span> Dog();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; animal-&gt;m_age &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    animal-&gt;run();</span><br><span class="line">    animal-&gt;speak();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo5&gt; ./demo1.exe</span><br><span class="line"><span class="number">0</span></span><br><span class="line">Dog::run()</span><br><span class="line">Dog::speak()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="11-6-多继承"><a href="#11-6-多继承" class="headerlink" title="11.6 多继承"></a>11.6 多继承</h4><h5 id="11-6-1-多继承概念"><a href="#11-6-1-多继承概念" class="headerlink" title="11.6.1 多继承概念"></a>11.6.1 多继承概念</h5><ul>
<li>C++允许一个类，继承多个类（<code>不建议使用</code>）</li>
<li>如果子类继承的<code>多个父类</code>都有虚函数，那么子类对象会产生对应的<code>多张虚函数表</code></li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Worker</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_salary;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Undergraduate</span> :</span> <span class="keyword">public</span> Student, <span class="keyword">public</span> Worker</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_grade;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Undergraduate under;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;sizeof: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(under) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    under.m_score = <span class="number">100</span>;</span><br><span class="line">    under.m_salary = <span class="number">2000</span>;</span><br><span class="line">    under.m_grade = <span class="number">4</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo5&gt; ./demo2.exe</span><br><span class="line">sizeof: <span class="number">12</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>内存布局：</p>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>内存地址</th>
<th>内存数据</th>
</tr>
</thead>
<tbody><tr>
<td><code>&amp;under</code></td>
<td><code>&amp;m_score</code></td>
<td><code>0x00E69B60</code></td>
<td>100</td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B61</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B62</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B63</td>
<td></td>
</tr>
<tr>
<td></td>
<td><code>&amp;m_salary</code></td>
<td><code>0x00E69B64</code></td>
<td>2000</td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B65</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B66</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B67</td>
<td></td>
</tr>
<tr>
<td></td>
<td><code>&amp;m_grade</code></td>
<td><code>0x00E69B68</code></td>
<td>4</td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B69</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B6A</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>0x00E69B6B</td>
<td></td>
</tr>
</tbody></table>
</li>
</ul>
<h5 id="11-6-2-同名成员函数-变量"><a href="#11-6-2-同名成员函数-变量" class="headerlink" title="11.6.2 同名成员函数/变量"></a>11.6.2 同名成员函数/变量</h5><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal_A</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal_A::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal_B</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Animal_B::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> :</span> <span class="keyword">public</span> Animal_A, <span class="keyword">public</span> Animal_B</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Dog::run()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Dog dog;</span><br><span class="line"></span><br><span class="line">    dog.m_age = <span class="number">10</span>;</span><br><span class="line">    dog.Animal_A::m_age = <span class="number">11</span>;</span><br><span class="line">    dog.Animal_B::m_age = <span class="number">12</span>;</span><br><span class="line">    dog.Dog::m_age = <span class="number">13</span>;</span><br><span class="line">    </span><br><span class="line">    dog.run();</span><br><span class="line">    dog.Animal_A::run();</span><br><span class="line">    dog.Animal_B::run();</span><br><span class="line">    dog.Dog::run();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo5&gt; ./demo3.exe</span><br><span class="line">Dog::run()</span><br><span class="line">Animal_A::run()</span><br><span class="line">Animal_B::run()</span><br><span class="line">Dog::run()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h5 id="11-6-3-菱形继承"><a href="#11-6-3-菱形继承" class="headerlink" title="11.6.3 菱形继承"></a>11.6.3 菱形继承</h5><h6 id="11-6-3-1-菱形继承结构"><a href="#11-6-3-1-菱形继承结构" class="headerlink" title="11.6.3.1 菱形继承结构"></a>11.6.3.1 菱形继承结构</h6><ul>
<li>基本结构：<ul>
<li>定义一个类：A</li>
<li>定义类B1，继承A；定义类B2，继承A</li>
<li>定义类C，继承B1，B2</li>
</ul>
</li>
</ul>
<ul>
<li>菱形继承的问题：<ul>
<li>最底层的对象，从基类继承的成员变量冗余，重复</li>
<li>最底层的对象，无法访问基类的成员变量，存在二义性</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> a;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B1</span> :</span> A <span class="comment">// 继承A的成员变量，2*int = 8字节</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> b1;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B2</span> :</span> A <span class="comment">// 同理，2*int = 8字节</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> b2;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> :</span> B1, B2 <span class="comment">// 继承B1，B2，加上自身成员变量，2+2+1 int = 20字节</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> c;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    C c;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;sizeof: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(c) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo5&gt; ./demo4.exe</span><br><span class="line">sizeof: <span class="number">20</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h6 id="11-6-3-2-虚继承"><a href="#11-6-3-2-虚继承" class="headerlink" title="11.6.3.2 虚继承"></a>11.6.3.2 虚继承</h6><ul>
<li>解决菱形继承带来的底层对象的成员变量冗余，重复和二义性</li>
</ul>
<ul>
<li>基本结构：<ul>
<li>定义一个类：A</li>
<li>定义类B1，继承 <code>virtual A</code>；定义类B2，继承 <code>virtual A</code></li>
<li>定义类C，继承B1，B2</li>
</ul>
</li>
<li>此时，A为 <code>虚基类</code></li>
<li>作用：B1，B2会共同继承同一份 A 的成员，而不是各自继承一份，解决了底层对象的成员冗余</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">A</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> a;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">B1</span> :</span> <span class="keyword">virtual</span> A <span class="comment">// 虚表8字节，成员变量4字节，12</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> b1;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">B2</span> :</span> <span class="keyword">virtual</span> A <span class="comment">// 虚表8字节，成员变量4字节，12</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> b2;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">C</span> :</span> B1, B2 <span class="comment">// 2*12 = 24，虚表8字节，成员变量4字节，虚基类成员变量4字节 = 24 + 8 + 4 + 4 = 40</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> c;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    C c;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;sizeof: &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(c) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo5&gt; ./demo4.exe</span><br><span class="line">sizeof: <span class="number">40</span></span><br></pre></td></tr></table></figure></li>
<li><p>虚继承的类结构：</p>
<ul>
<li>首地址存放虚指针和偏移量</li>
<li>存放自己的成员变量</li>
<li>存放虚基类的成员变量</li>
</ul>
</li>
</ul>
<h3 id="12-静态成员"><a href="#12-静态成员" class="headerlink" title="12. 静态成员"></a>12. 静态成员</h3><h4 id="12-1-静态成员基本定义"><a href="#12-1-静态成员基本定义" class="headerlink" title="12.1 静态成员基本定义"></a>12.1 静态成员基本定义</h4><ul>
<li>定义：<code>static</code>修饰的成员变量/函数<ul>
<li>可以通过<code>对象.静态成员</code>，<code>对象指针-&gt;静态成员</code>，<code>类名::静态成员变量</code>，进行访问</li>
</ul>
</li>
<li>静态成员变量：<ul>
<li>存储在数据段（<code>全局区，类似全局变量</code>），整个程序运行过程中只有一份内存</li>
<li>对比全局变量，它可以设定访问权限（<code>public</code>, <code>protected</code>, <code>private</code>），达到局部共享的目的</li>
<li>必须初始化，必须在类外面初始化，初始化不能带<code>static</code>，如果类的声明和实现分离（<code>在实现中初始化</code>）</li>
</ul>
</li>
<li>静态成员函数：<ul>
<li>内部不能使用<code>this</code>指针（<code>this</code>指针只能用于非静态成员函数内部）</li>
<li>不能是虚函数（<code>虚函数只能是非静态成员函数</code>）</li>
<li>内部不能访问非静态成员变量\函数，只能访问静态成员变量\函数</li>
<li>构造函数和析构函数不可以是静态的</li>
<li>当声明和实现分离，实现不能带<code>static</code></li>
</ul>
</li>
<li>总结：<ul>
<li>涉及到，通过<code>对象</code>来管理，调用成员的变量和函数，都不可以用<code>static</code>修饰</li>
<li><code>static</code>修饰的成员变量/函数只能通过类，利用作用域使用，或者在静态的函数内使用静态成员变量</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> m_price;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">byCar</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> Car::m_price = <span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Car::byCar</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car is running&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car price: &quot;</span> &lt;&lt; Car::m_price &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    Car::byCar();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo6&gt; .\demo1.exe</span><br><span class="line">Car price: <span class="number">1000</span></span><br><span class="line">Car is running</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="12-2-反汇编分析静态成员"><a href="#12-2-反汇编分析静态成员" class="headerlink" title="12.2 反汇编分析静态成员"></a>12.2 反汇编分析静态成员</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> m_price;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> Car::m_price = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> g_age;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car1;</span><br><span class="line">    Car car2;</span><br><span class="line">    </span><br><span class="line">    g_age = <span class="number">0</span>;</span><br><span class="line">    car1.m_age = <span class="number">1</span>;</span><br><span class="line">    car1.m_price = <span class="number">2</span>;</span><br><span class="line">    car2.m_price = <span class="number">3</span>;</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编分析，静态成员变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; g_age &#x3D; 0;</span><br><span class="line">mov dword ptr ds:[00A9A318h], 0	&#x3D;&gt; ds 数据段</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; car1.m_age &#x3D; 1;</span><br><span class="line">mov dword ptr[car], 1	&#x3D;&gt; mov dword ptr[ebp-8], 1</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; car1.m_price &#x3D; 2;</span><br><span class="line">mov dword ptr[Car::m_price 00A9A314h)], 2	&#x3D;&gt; mov dword ptr ds:[00A9A314h], 2</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; car2.m_price &#x3D; 3;</span><br><span class="line">mov dword ptr[Car::m_price (00A9A314h)], 3	&#x3D;&gt; mov dword ptr ds:[00A9A314h], 3</span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>全局变量和类中的静态变量，都存放在数据区内，且程序运行时，仅有一份</li>
<li>静态变量和全局变量的区别：<ul>
<li>静态变量可以在类中定义，同时通过<code>public</code>, <code>protected</code>, <code>private</code>修饰其访问权限</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="12-3-静态成员应用"><a href="#12-3-静态成员应用" class="headerlink" title="12.3 静态成员应用"></a>12.3 静态成员应用</h4><h5 id="12-3-1-统计对象个数"><a href="#12-3-1-统计对象个数" class="headerlink" title="12.3.1 统计对象个数"></a>12.3.1 统计对象个数</h5><ul>
<li><p>应用：无论在那个区域增加或删除对象，都能有一个唯一值去统计当前的对象个数</p>
</li>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> ms_count;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car()&#123;++ms_count;&#125;</span><br><span class="line">    ~Car()&#123;--ms_count;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> ms_count;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">int</span> Car::ms_count = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">Car g_car; <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car; <span class="comment">// 2</span></span><br><span class="line">    Car *p_car = <span class="keyword">new</span> Car; <span class="comment">// 3</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; Car::getCount() &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo6&gt; .\demo2.exe</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h5 id="12-3-2-单例模式"><a href="#12-3-2-单例模式" class="headerlink" title="12.3.2 单例模式"></a>12.3.2 单例模式</h5><ul>
<li>单例模式的构建：<ul>
<li>构造/析构函数，私有化</li>
<li>定义一个私有化的 static成员变量指向唯一得到单例对象</li>
<li>提供一个公共的访问单例对象的接口</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Rocket</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="comment">// 构造/析构函数，私有化</span></span><br><span class="line">    Rocket()&#123;&#125;</span><br><span class="line">    ~Rocket()&#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义一个私有化的 static成员变量指向唯一得到单例对象</span></span><br><span class="line">    <span class="keyword">static</span> Rocket* ms_rocket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 提供一个公共的访问单例对象的接口</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> Rocket* <span class="title">sharedRocket</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 需要考虑多线程安全</span></span><br><span class="line">        <span class="keyword">if</span> (ms_rocket == <span class="literal">NULL</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            ms_rocket = <span class="keyword">new</span> Rocket();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ms_rocket;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提供一个公共的访问单例对象的接口</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteRocket</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 需要考虑多线程安全</span></span><br><span class="line">        <span class="keyword">if</span> (ms_rocket != <span class="literal">NULL</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            ms_rocket = <span class="literal">NULL</span>;</span><br><span class="line">            <span class="keyword">delete</span> ms_rocket;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试用函数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">startRocket</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Start Rocket&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">&#125;;</span><br><span class="line">Rocket* Rocket::ms_rocket = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Rocket *p_rocket_1 = Rocket::sharedRocket();</span><br><span class="line">    Rocket *p_rocket_2 = Rocket::sharedRocket();</span><br><span class="line">    Rocket *p_rocket_3 = Rocket::sharedRocket();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p_rocket_1 &lt;&lt; <span class="string">&quot;\n&quot;</span> &lt;&lt; p_rocket_2 &lt;&lt; <span class="string">&quot;\n&quot;</span> &lt;&lt; p_rocket_3 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    p_rocket_1-&gt;startRocket();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo6&gt; .\demo3.exe</span><br><span class="line"><span class="number">0</span>x8e6b80</span><br><span class="line"><span class="number">0</span>x8e6b80</span><br><span class="line"><span class="number">0</span>x8e6b80</span><br><span class="line"><span class="built_in">Start</span> Rocket</span><br></pre></td></tr></table></figure></li>
<li><p>总结：由打印结果可看出，指向堆区的对象指针的地址始终不变，对象的单例模型已经实现</p>
</li>
</ul>
<ul>
<li>补充：delete<ul>
<li><code>new</code>是堆空间，开辟一块区域，用来存放数据</li>
<li><code>delete</code>是删除堆空间开辟的区域，使得这片区域<code>可以被别的new使用</code>，但里面的数据<code>没有变化</code>，</li>
<li>需要在delete之前或之后，使其初始化为<code>null</code></li>
</ul>
</li>
</ul>
<h3 id="13-const成员"><a href="#13-const成员" class="headerlink" title="13. const成员"></a>13. const成员</h3><ul>
<li>被<code>const修饰</code>的<code>成员</code>变量、<code>非静态</code>成员<code>函数</code></li>
</ul>
<ul>
<li><code>const</code>成员变量：<ul>
<li><code>必须</code>在<code>类内部初始化</code>，可以在声明时，直接赋值</li>
<li><code>非static</code>的<code>const成员变量</code>还可以在初始化列表中初始化</li>
</ul>
</li>
</ul>
<ul>
<li><code>const</code>成员函数：<ul>
<li><code>const</code>关键字写在参数列表后面，函数的声明和实现都要写<code>const</code><ul>
<li>内部不能修改<code>非static</code>成员变量</li>
<li>内部只能调用<code>const修饰</code>的成员函数，<code>static</code>成员函数</li>
<li><code>非const</code>成员函数可以调用<code>const</code>成员函数</li>
</ul>
</li>
<li><code>const</code>成员函数和<code>非const</code>成员函数<code>构成重载</code><ul>
<li><code>非const</code>对象（<code>指针</code>）优先调用<code>非const</code>成员函数</li>
</ul>
</li>
<li><code>const</code>对象（<code>指针</code>）只能调用<code>const</code>成员函数，<code>static</code>成员函数</li>
</ul>
</li>
</ul>
<h3 id="14-引用类型成员"><a href="#14-引用类型成员" class="headerlink" title="14. 引用类型成员"></a>14. 引用类型成员</h3><ul>
<li>引用类型成员变量必须初始化（不考虑static）<ul>
<li>在声明的时候直接初始化</li>
<li>通过初始化列表初始化</li>
</ul>
</li>
</ul>
<h3 id="15-拷贝构造函数"><a href="#15-拷贝构造函数" class="headerlink" title="15. 拷贝构造函数"></a>15. 拷贝构造函数</h3><ul>
<li>拷贝构造函数是构造函数的一种</li>
<li>当利用已经存在的对象创建一个新对象时（类似于拷贝），就会调用对象的拷贝构造函数进行初始化</li>
<li>拷贝构造函数的格式是固定的，接收一个<code>const</code>修饰的<code>引用类型</code>参数</li>
</ul>
<h4 id="15-1-默认拷贝构造函数"><a href="#15-1-默认拷贝构造函数" class="headerlink" title="15.1 默认拷贝构造函数"></a>15.1 默认拷贝构造函数</h4><ul>
<li><p>代码示例：(默认拷贝构造函数)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line">    <span class="keyword">int</span> m_length;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car(<span class="keyword">int</span> price = <span class="number">0</span>, <span class="keyword">int</span> lenght = <span class="number">0</span>) : m_price(price), m_length(lenght)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">display</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;price:&quot;</span> &lt;&lt; m_price &lt;&lt; <span class="string">&quot; length: &quot;</span> &lt;&lt; m_length &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Car <span class="title">car1</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    car1.display();</span><br><span class="line"></span><br><span class="line">    <span class="function">Car <span class="title">car2</span><span class="params">(car1)</span></span>;</span><br><span class="line">    car2.display();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo1.exe</span><br><span class="line">Car::Car()</span><br><span class="line">price:<span class="number">1</span> length: <span class="number">1</span></span><br><span class="line">price:<span class="number">1</span> length: <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看原理：（默认构造函数）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; x86环境</span><br><span class="line">&#x2F;&#x2F; Car car2(car1);</span><br><span class="line">mov eax, dword ptr[ebp-10h] &#x3D;&gt; 10h &#x3D; 16</span><br><span class="line">mov dword ptr[ebp-20h], eax &#x3D;&gt; 20h &#x3D; 32</span><br><span class="line">&#x2F;&#x2F; &#x3D;&gt; car2.m_prcie &#x3D; car1.m_price;</span><br><span class="line"></span><br><span class="line">mov ecx, dword ptr[ebp-0Ch] &#x3D;&gt; 0Ch &#x3D; 12</span><br><span class="line">mov dword ptr[ebo-1Ch], ecx &#x3D;&gt; 1Ch &#x3D; 28</span><br><span class="line">&#x2F;&#x2F; &#x3D;&gt; car2.m_length &#x3D; car1.m_length;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; car2.display();</span><br><span class="line">lea ecx, [ebp-20h]</span><br><span class="line">call 0024141A</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; x64环境</span><br><span class="line">&#x2F;&#x2F; Car car2(car1);</span><br><span class="line">mov rax, qword ptr[rbp+8] &#x3D;&gt; 8h &#x3D; 8</span><br><span class="line">mov qword ptr[rbp+28h], rax &#x3D;&gt; 28h &#x3D; 40</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; car2.display();</span><br><span class="line">lea rcx, [rbp+28h]</span><br><span class="line">call 00007FF685AF12FD</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="15-2-自定义拷贝构造函数"><a href="#15-2-自定义拷贝构造函数" class="headerlink" title="15.2 自定义拷贝构造函数"></a>15.2 自定义拷贝构造函数</h4><ul>
<li><p>代码示例：（自定义拷贝构造函数）</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line">    <span class="keyword">int</span> m_length;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car(<span class="keyword">int</span> price = <span class="number">0</span>, <span class="keyword">int</span> lenght = <span class="number">0</span>) : m_price(price), m_length(lenght)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">    Car(<span class="keyword">const</span> Car &amp;car) : m_price(car.m_price), m_length(car.m_length)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(const)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">display</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;price:&quot;</span> &lt;&lt; m_price &lt;&lt; <span class="string">&quot; length: &quot;</span> &lt;&lt; m_length &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Car <span class="title">car1</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    car1.display();</span><br><span class="line"></span><br><span class="line">    <span class="function">Car <span class="title">car2</span><span class="params">(car1)</span></span>;</span><br><span class="line">    car2.display();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo1.exe</span><br><span class="line">Car::Car()</span><br><span class="line">price:<span class="number">1</span> length: <span class="number">1</span></span><br><span class="line">Car::Car(const)</span><br><span class="line">price:<span class="number">1</span> length: <span class="number">1</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="15-3-调用父类拷贝构造函数"><a href="#15-3-调用父类拷贝构造函数" class="headerlink" title="15.3 调用父类拷贝构造函数"></a>15.3 调用父类拷贝构造函数</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    Person(<span class="keyword">int</span> age) : m_age(age)&#123;&#125;;</span><br><span class="line">    Person(<span class="keyword">const</span> Person &amp;person) : m_age(person.m_age)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">    </span><br><span class="line">    Student(<span class="keyword">int</span> age, <span class="keyword">int</span> score) : Person(age), m_score(score)&#123;&#125;       </span><br><span class="line">    Student(<span class="keyword">const</span> Student &amp;student) : Person(student), m_score(student.m_score)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Student <span class="title">stu1</span><span class="params">(<span class="number">1</span>, <span class="number">100</span>)</span></span>;</span><br><span class="line">    <span class="function">Student <span class="title">stu2</span><span class="params">(stu1)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; stu2.m_age &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; stu2.m_score &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo2.exe</span><br><span class="line"><span class="number">1</span> <span class="number">100</span></span><br></pre></td></tr></table></figure></li>
<li><p>补充：如果是完全拷贝对象内的数据，可以直接使用默认拷贝构造函数</p>
</li>
</ul>
<h4 id="15-4-浅复制"><a href="#15-4-浅复制" class="headerlink" title="15.4 浅复制"></a>15.4 浅复制</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">    </span><br><span class="line">    Student(<span class="keyword">int</span> score = <span class="number">0</span>) : m_score(score)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::Student()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;       </span><br><span class="line">    Student(<span class="keyword">const</span> Student &amp;student) : m_score(student.m_score)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Student::Student(const Student &amp;student)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 默认构造</span></span><br><span class="line">    <span class="function">Student <span class="title">s1</span><span class="params">(<span class="number">10</span>)</span></span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 拷贝构造</span></span><br><span class="line">    <span class="function">Student <span class="title">s2</span><span class="params">(s1)</span></span>;</span><br><span class="line">    <span class="comment">// 拷贝构造 =&gt; 创建对象的同时，将 s2 的数据给 s3，符合拷贝构造</span></span><br><span class="line">    Student s3 = s2;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 浅复制 =&gt; 创建了对象，但没有利用已经存在的对象进赋值，所以是 默认构造</span></span><br><span class="line">    Student s4;</span><br><span class="line">    s4 = s3; <span class="comment">// 此时的两个对象都是 已经存在的对象，不是拷贝g</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo3.exe</span><br><span class="line">Student::Student()</span><br><span class="line">Student::Student(const Student &amp;student)</span><br><span class="line">Student::Student(const Student &amp;student)</span><br><span class="line">Student::Student()</span><br></pre></td></tr></table></figure>







</li>
</ul>
<h4 id="15-5-浅-深拷贝"><a href="#15-5-浅-深拷贝" class="headerlink" title="15.5 浅/深拷贝"></a>15.5 浅/深拷贝</h4><h5 id="15-5-1-解决浅拷贝问题"><a href="#15-5-1-解决浅拷贝问题" class="headerlink" title="15.5.1 解决浅拷贝问题"></a>15.5.1 解决浅拷贝问题</h5><ul>
<li>编译器默认的都是浅拷贝</li>
<li>浅拷贝的缺点：<ul>
<li>如果出现堆空间指向栈空间，那么浅拷贝后，堆空间内存储着栈空间的地址</li>
<li>栈空间的生命周期是不可控制的，可能会使得堆空间指向栈空间的指针变成野指针</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：解决浅拷贝的问题</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line">    <span class="keyword">char</span> *m_name;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car(<span class="keyword">int</span> price = <span class="number">0</span>, <span class="keyword">const</span> <span class="keyword">char</span> *name = <span class="literal">nullptr</span>) : m_price(price)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (name == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line"></span><br><span class="line">        m_name = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="built_in">strlen</span>(name) + <span class="number">1</span>]&#123;&#125;;</span><br><span class="line">        <span class="built_in">strcpy</span>(m_name, name);</span><br><span class="line">    &#125;    </span><br><span class="line">    ~Car()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (m_name == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">        m_name = <span class="literal">nullptr</span>;</span><br><span class="line">        <span class="keyword">delete</span>[] m_name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">printInfo</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; m_price &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_name &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> name[] = &#123;<span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="string">&#x27;\0&#x27;</span>&#125;;</span><br><span class="line">    Car *car = <span class="keyword">new</span> Car(<span class="number">100</span>, name);</span><br><span class="line">    car-&gt;printInfo();</span><br><span class="line"></span><br><span class="line">    Car *car2 = <span class="keyword">new</span> Car(<span class="number">200</span>, <span class="string">&quot;bwm&quot;</span>);</span><br><span class="line">    car2-&gt;printInfo();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo4.exe</span><br><span class="line"><span class="number">100</span> bwm</span><br><span class="line"><span class="number">200</span> bwm</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h5 id="15-5-2-深拷贝"><a href="#15-5-2-深拷贝" class="headerlink" title="15.5.2 深拷贝"></a>15.5.2 深拷贝</h5><ul>
<li>使用深拷贝的原因：<ul>
<li>当类中有数据会在堆空间中开辟，且在类的对象析构时释放时</li>
<li>此时这个类已有一个对象，而新的对象利用已有的对象进行默认的拷贝构造（浅拷贝）</li>
<li>此时，两个对象的某一个指向堆空间的成员变量，所指的地址相同</li>
<li>当其中一个对象释放，另一个对象中的某个指向堆空间的成员变量也会被释放，使得另一个对象中的成员变量无效化</li>
<li>如果两个对象都释放，则会出现同一个堆空间，两次释放</li>
</ul>
</li>
<li>深拷贝定义：<ul>
<li>将指针指向的内容拷贝到新的存储空间</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_price;</span><br><span class="line">    <span class="keyword">char</span> *m_name;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">copy</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *name = <span class="literal">nullptr</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (name == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line"></span><br><span class="line">        m_name = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="built_in">strlen</span>(name) + <span class="number">1</span>]&#123;&#125;;</span><br><span class="line">        <span class="built_in">strcpy</span>(m_name, name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car(<span class="keyword">int</span> price = <span class="number">0</span>, <span class="keyword">const</span> <span class="keyword">char</span> *name = <span class="literal">nullptr</span>) : m_price(price)&#123;copy(name);&#125;    </span><br><span class="line">    Car(<span class="keyword">const</span> Car &amp;car) : m_price(car.m_price)&#123;copy(car.m_name);&#125;</span><br><span class="line">    ~Car()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (m_name == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">        m_name = <span class="literal">nullptr</span>;</span><br><span class="line">        <span class="keyword">delete</span>[] m_name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">printInfo</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; m_price &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_name &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Car <span class="title">car1</span><span class="params">(<span class="number">100</span>, <span class="string">&quot;bwm&quot;</span>)</span></span>;</span><br><span class="line">    Car car2 = car1;</span><br><span class="line">    car2.printInfo();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo5.exe</span><br><span class="line"><span class="number">100</span> bwm</span><br></pre></td></tr></table></figure>








</li>
</ul>
<h3 id="16-对象类型参数和返回值"><a href="#16-对象类型参数和返回值" class="headerlink" title="16. 对象类型参数和返回值"></a>16. 对象类型参数和返回值</h3><ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    Car(<span class="keyword">const</span> Car &amp;car)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(const)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">(Car car)</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car1;</span><br><span class="line">    test(car1);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo6.exe</span><br><span class="line">Car::Car() // Car car1;</span><br><span class="line">Car::Car(const) // Car car = car1;</span><br></pre></td></tr></table></figure>






</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car() &quot;</span>&lt;&lt; <span class="keyword">this</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">    Car(<span class="keyword">const</span> Car &amp;car)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(const) &quot;</span> &lt;&lt; <span class="keyword">this</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test1</span><span class="params">(Car car)</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">Car <span class="title">test2</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car;</span><br><span class="line">    <span class="keyword">return</span> car;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car;</span><br><span class="line">    test1(car);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    Car car1;</span><br><span class="line">    car1 = test2();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo7&gt; .\demo6.exe</span><br><span class="line">Car::Car() <span class="number">0</span>x7bfe1d</span><br><span class="line">Car::Car(const) <span class="number">0</span>x7bfe1e</span><br><span class="line"></span><br><span class="line">Car::Car() <span class="number">0</span>x7bfe1c</span><br><span class="line">Car::Car() <span class="number">0</span>x7bfe1f</span><br></pre></td></tr></table></figure>







</li>
</ul>
<h3 id="17-匿名对象"><a href="#17-匿名对象" class="headerlink" title="17. 匿名对象"></a>17. 匿名对象</h3><ul>
<li>又名：临时对象</li>
<li>没有名称，不被指针指向，一次性使用，立即销毁</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    Car(<span class="keyword">const</span> Car &amp;car)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(const)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">    ~Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::~Car()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">(Car car)</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    test(Car());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo8&gt; .\demo1.exe</span><br><span class="line">Car::Car()</span><br><span class="line">Car::~Car()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="18-隐式构造"><a href="#18-隐式构造" class="headerlink" title="18. 隐式构造"></a>18. 隐式构造</h3><ul>
<li><p>又名：转换构造</p>
</li>
<li><p>在某些情况下，会隐式调用单参数的构造函数</p>
</li>
<li><p>通过关键字<code>explicit</code>禁用隐式构造</p>
</li>
</ul>
<ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;    </span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    Person(<span class="keyword">int</span> age) : m_age(age)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(int)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    ~Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">(Person person)</span></span>&#123;&#125;</span><br><span class="line"><span class="function">Person <span class="title">test2</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> <span class="number">40</span>;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person p1 = <span class="number">20</span>;</span><br><span class="line">    test2();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo8&gt; g++ demo2.cpp <span class="literal">-o</span> demo2; .\demo2</span><br><span class="line">Car::Car(int)</span><br><span class="line">Car::Car(int)</span><br><span class="line">Person::~Person()</span><br><span class="line">Person::~Person()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;    </span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">Person</span><span class="params">(<span class="keyword">int</span> age)</span> : <span class="title">m_age</span><span class="params">(age)</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car(int)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    ~Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">(Person person)</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// Person p1 = 20; // 此时这样的写法被 禁用</span></span><br><span class="line">    <span class="function">Person <span class="title">p1</span><span class="params">(<span class="number">20</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo8&gt; g++ demo3.cpp <span class="literal">-o</span> demo3; .\demo3</span><br><span class="line">Car::Car(int)</span><br><span class="line">Person::~Person()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="19-编译器自动生成的构造函数"><a href="#19-编译器自动生成的构造函数" class="headerlink" title="19. 编译器自动生成的构造函数"></a>19. 编译器自动生成的构造函数</h3><ul>
<li>C++编译器会在特定的情况下，会给类自动生成无参的构造函数<ul>
<li>成员变量在声明时，初始化</li>
<li>有定义虚函数</li>
<li>虚继承了其它类</li>
<li>包含了对象类型的成员，且这个成员有构造函数（可以是编译器自动生成的，或自定义的）</li>
</ul>
</li>
<li>总结：<ul>
<li>对象创建后，需要进行一些额外的操作（内存操作，函数调用，成员变量初始化，虚函数，虚继承，调用父类构造等等）</li>
<li>编译器都会自动生成无参的构造函数</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例1：成员变量在声明时，初始化</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age = <span class="number">5</span>;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 此段代码 完全等价 上面的写法</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">class Car</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">public:</span></span><br><span class="line"><span class="comment">    int m_age = 5;    </span></span><br><span class="line"><span class="comment">    Car()&#123;m_age = 5;&#125;</span></span><br><span class="line"><span class="comment">&#125;;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Car car;</span><br><span class="line">lea ecx, [car]</span><br><span class="line">call Car::Car(0A1384h)</span><br><span class="line"></span><br><span class="line">mov dword ptr[eax], 5</span><br><span class="line">mov eax, dword ptr[this]</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：有定义虚函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;  </span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Car car;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">lea ecx, [car]</span><br><span class="line">call Car::Car(0E5111Dh)</span><br><span class="line"></span><br><span class="line">mov dword ptr[this], ecx</span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov dword ptr[eax], offset Car::&#96;vftable&#96;(0E57B34h) &#x2F;&#x2F; vftable &#x3D;&#x3D; virtual function table 虚函数表</span><br><span class="line">mov eax, dword ptr[this]</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例3：虚继承了其它类</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;  </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">virtual</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Student student;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">lea ecx, [student]</span><br><span class="line">call Student::Student(012213A7h)</span><br><span class="line"></span><br><span class="line">mov eax, dword ptr[this]</span><br><span class="line">mov dword ptr[eax], offset Student::&#96;vftable&#96;(01227B30h)</span><br><span class="line">mov eax, dword ptr[this]</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="20-友元"><a href="#20-友元" class="headerlink" title="20. 友元"></a>20. 友元</h3><ul>
<li>友元包括：<ul>
<li>友元函数<ul>
<li>如果将函数A（非成员函数）声明为类B的友元函数，那么函数A内部可以直接访问类B对象的所有成员</li>
</ul>
</li>
<li>友元类<ul>
<li>类A内声明了类B的友元，类B可以访问类A的所有成员</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例1：友元函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 声明 友元函数</span></span><br><span class="line">    <span class="function"><span class="keyword">friend</span> Point <span class="title">add_P</span><span class="params">(Point &amp;, Point &amp;)</span></span>;  </span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">printPoint</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; m_x &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_y &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实现 友元函数</span></span><br><span class="line"><span class="function">Point <span class="title">add_P</span><span class="params">(Point &amp;p1, Point &amp;p2)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Point <span class="title">p1</span><span class="params">(<span class="number">10</span>, <span class="number">10</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p2</span><span class="params">(<span class="number">20</span>, <span class="number">20</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p3</span><span class="params">(add_P(p1, p2))</span></span>;</span><br><span class="line">    p3.printPoint();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo8&gt; .\demo5</span><br><span class="line"><span class="number">30</span> <span class="number">30</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：友元类</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 声明 友元类</span></span><br><span class="line">    <span class="keyword">friend</span> <span class="class"><span class="keyword">class</span> <span class="title">FH_Math</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">printPoint</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; m_x &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_y &lt;&lt; <span class="built_in">endl</span>;&#125;    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实现 友元类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FH_Math</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="function">Point <span class="title">add_P</span><span class="params">(Point &amp;p1, Point &amp;p2)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Point <span class="title">p1</span><span class="params">(<span class="number">10</span>, <span class="number">10</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p2</span><span class="params">(<span class="number">20</span>, <span class="number">20</span>)</span></span>;</span><br><span class="line">    Point p3 = FH_Math().add_P(p1, p2);</span><br><span class="line">    p3.printPoint();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo8&gt; .\demo6</span><br><span class="line"><span class="number">30</span> <span class="number">30</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h3 id="21-内部类"><a href="#21-内部类" class="headerlink" title="21. 内部类"></a>21. 内部类</h3><h4 id="21-1-内部类的定义"><a href="#21-1-内部类的定义" class="headerlink" title="21.1 内部类的定义"></a>21.1 内部类的定义</h4><ul>
<li>定义：<ul>
<li>类B定义在类A中，B就是A的内部类（嵌套类）</li>
<li>类A就是B的外部类</li>
</ul>
</li>
<li>特点：<ul>
<li>支持：<code>public</code>, <code>protected</code>, <code>private</code>权限</li>
<li>内部类成员函数可以访问外部类所有成员</li>
<li>外部类则无法访问内部类的成员</li>
<li>成员函数可以直接不带类名，对象名访问其外部类的<code>static修饰</code>的成员</li>
<li>不会影响外部类的内存布局</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::test1()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::test2(static)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">    <span class="keyword">private</span>:</span><br><span class="line">        <span class="keyword">int</span> m_id;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span>:        </span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;test2();&#125;        </span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person::Student s;</span><br><span class="line">    s.test3();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="keyword">sizeof</span>(Person) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="keyword">sizeof</span>(Person::Student) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\demo9&gt; .\demo1</span><br><span class="line">Person::test2(<span class="keyword">static</span>)</span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="21-2-内部类声明与实现分离"><a href="#21-2-内部类声明与实现分离" class="headerlink" title="21.2 内部类声明与实现分离"></a>21.2 内部类声明与实现分离</h4><ul>
<li>跟正常的函数声明和实现分离差不多</li>
<li>标明类的作用域就可以了</li>
</ul>
<h3 id="22-局部类"><a href="#22-局部类" class="headerlink" title="22. 局部类"></a>22. 局部类</h3><ul>
<li>在一个函数中定义一个类：局部类</li>
<li>特点：<ul>
<li>作用域仅限于所在的函数内部，不允许<code>static修饰</code>成员</li>
<li>局部类的成员函数不能直接访问所在函数的局部变量（<code>static</code>可以）</li>
</ul>
</li>
</ul>
<h3 id="23-运算符重载"><a href="#23-运算符重载" class="headerlink" title="23. 运算符重载"></a>23. 运算符重载</h3><ul>
<li>注意：<ul>
<li>有些运算符不可以重载：<ul>
<li>对象成员访问运算符<code>.</code></li>
<li>域运算符<code>::</code></li>
<li>三目运算符<code>?</code></li>
<li><code>sizeof</code></li>
</ul>
</li>
<li>有些运算符只能重载为成员函数：<ul>
<li>赋值运算符<code>=</code></li>
<li>下标运算符<code>[]</code></li>
<li>函数运算符<code>()</code></li>
<li>指针访问成员<code>-&gt;</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="23-1-基本运算符重载"><a href="#23-1-基本运算符重载" class="headerlink" title="23.1 基本运算符重载"></a>23.1 基本运算符重载</h4><ul>
<li>作用：给运算符增加一些新的功能</li>
<li>设计思路：<ul>
<li>如果仅和类相关，重载运算符函数优先写在类里面</li>
<li>如果是<code>&lt;&lt;</code>，<code>&gt;&gt;</code>的重载，尽量写成全局函数</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例1：<code>+, -</code>, <code>+=, -=</code>, <code>==, !=</code>, <code>-</code>, <code>++, --</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">showPoint</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;(&quot;</span> &lt;&lt; m_x &lt;&lt; <span class="string">&quot;, &quot;</span> &lt;&lt; m_y &lt;&lt; <span class="string">&quot;)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;         </span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator +, -</span></span><br><span class="line">    <span class="keyword">const</span> Point <span class="keyword">operator</span>+(<span class="keyword">const</span> Point &amp;point) <span class="keyword">const</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> Point(<span class="keyword">this</span>-&gt;m_x + point.m_x, <span class="keyword">this</span>-&gt;m_y + point.m_y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator +=, -=</span></span><br><span class="line">    Point &amp;<span class="keyword">operator</span>+=(<span class="keyword">const</span> Point &amp;point)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;m_x += point.m_x;</span><br><span class="line">        <span class="keyword">this</span>-&gt;m_y += point.m_y;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator ==, !=</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">bool</span> <span class="keyword">operator</span>==(<span class="keyword">const</span> Point &amp;point)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">this</span>-&gt;m_x == point.m_x &amp;&amp; <span class="keyword">this</span>-&gt;m_y == point.m_y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator -</span></span><br><span class="line">    <span class="keyword">const</span> Point <span class="keyword">operator</span>-() <span class="keyword">const</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> Point(-m_x, -m_y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator ++, --</span></span><br><span class="line">    Point &amp;<span class="keyword">operator</span>++()</span><br><span class="line">    &#123;</span><br><span class="line">        ++<span class="keyword">this</span>-&gt;m_x;</span><br><span class="line">        ++<span class="keyword">this</span>-&gt;m_y;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> Point <span class="keyword">operator</span>++(<span class="keyword">int</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function">Point <span class="title">origin_P</span><span class="params">(<span class="keyword">this</span>-&gt;m_x, <span class="keyword">this</span>-&gt;m_y)</span></span>;</span><br><span class="line">        ++<span class="keyword">this</span>-&gt;m_x;</span><br><span class="line">        ++<span class="keyword">this</span>-&gt;m_y;</span><br><span class="line">        <span class="keyword">return</span> origin_P;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// operator +, -</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator +, -&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p1</span><span class="params">(<span class="number">10</span>, <span class="number">10</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p2</span><span class="params">(<span class="number">20</span>, <span class="number">20</span>)</span></span>;</span><br><span class="line">    Point p3 = p1 + p2;</span><br><span class="line">    Point p4 = p1 + p2 + p3;</span><br><span class="line">    p4.showPoint();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;---------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator +=, -=</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator +=, -=&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p5</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p6</span><span class="params">(<span class="number">2</span>, <span class="number">2</span>)</span></span>;</span><br><span class="line">    Point p7;</span><br><span class="line">    (p7 += p5) = p6;</span><br><span class="line">    p4.showPoint();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;---------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator ==, !=</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator ==, !=&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p8</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p9</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; (p1 == p2 ? <span class="string">&quot;true&quot;</span> : <span class="string">&quot;false&quot;</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;---------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator -</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator -&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p10</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p11</span><span class="params">(<span class="number">0</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">    Point p12;</span><br><span class="line">    p11 = -p10;</span><br><span class="line">    p10.showPoint();</span><br><span class="line">    p11.showPoint();</span><br><span class="line">    p12 = -(-p1);</span><br><span class="line">    p12.showPoint();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;---------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// operator ++, --</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;// operator ++, --&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p13</span><span class="params">(<span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p14</span><span class="params">(<span class="number">0</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">    <span class="function">Point <span class="title">p15</span><span class="params">(<span class="number">2</span>, <span class="number">2</span>)</span></span>;</span><br><span class="line">    ++p13;</span><br><span class="line">    p13++;</span><br><span class="line">    p13.showPoint();</span><br><span class="line">    p14 = (p13++) + p15;</span><br><span class="line">    p14.showPoint();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;---------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\demo9&gt; .\demo3</span><br><span class="line">operator +, -</span><br><span class="line">(<span class="number">60</span>, <span class="number">60</span>)</span><br><span class="line">---------</span><br><span class="line">operator +=, -=</span><br><span class="line">(<span class="number">60</span>, <span class="number">60</span>)</span><br><span class="line">---------</span><br><span class="line">operator ==, !=</span><br><span class="line">false</span><br><span class="line">---------</span><br><span class="line">operator -</span><br><span class="line">(<span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">(<span class="literal">-1</span>, <span class="literal">-1</span>)</span><br><span class="line">(<span class="number">10</span>, <span class="number">10</span>)</span><br><span class="line">---------</span><br><span class="line">// operator ++, --</span><br><span class="line">(<span class="number">3</span>, <span class="number">3</span>)</span><br><span class="line">(<span class="number">5</span>, <span class="number">5</span>)</span><br><span class="line">---------</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例2：<code>&lt;&lt;</code> <code>&gt;&gt;</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// operator &lt;&lt;, &gt;&gt;</span></span><br><span class="line">    <span class="keyword">friend</span> ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point);</span><br><span class="line">    <span class="keyword">friend</span> istream &amp;<span class="keyword">operator</span>&gt;&gt;(istream &amp;<span class="built_in">cin</span>, Point &amp;point);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;(&quot;</span> &lt;&lt; point.m_x &lt;&lt; <span class="string">&quot;, &quot;</span> &lt;&lt; point.m_y &lt;&lt; <span class="string">&quot;)&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">cout</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">istream &amp;<span class="keyword">operator</span>&gt;&gt;(istream &amp;<span class="built_in">cin</span>, Point &amp;point)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;input m_x: &quot;</span>;</span><br><span class="line">    <span class="built_in">cin</span> &gt;&gt; point.m_x;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;input m_y: &quot;</span>;</span><br><span class="line">    <span class="built_in">cin</span> &gt;&gt; point.m_y;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">cin</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// operator &lt;&lt;, &gt;&gt;</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator &lt;&lt;&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Point <span class="title">p1</span><span class="params">(<span class="number">10</span>, <span class="number">10</span>)</span></span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p1 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p1 &lt;&lt; p1 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;--------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;operator &gt;&gt;&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    Point p2;</span><br><span class="line">    Point p3;</span><br><span class="line">    <span class="built_in">cin</span> &gt;&gt; p2 &gt;&gt; p3;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; p2 &lt;&lt; <span class="string">&quot;\n&quot;</span> &lt;&lt; p3 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;--------&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\demo9&gt; .\demo4    </span><br><span class="line">operator &lt;&lt;</span><br><span class="line">(<span class="number">10</span>, <span class="number">10</span>)</span><br><span class="line">(<span class="number">10</span>, <span class="number">10</span>)(<span class="number">10</span>, <span class="number">10</span>)</span><br><span class="line">--------</span><br><span class="line">operator &gt;&gt;</span><br><span class="line">input m_x: <span class="number">1</span></span><br><span class="line">input m_y: <span class="number">1</span></span><br><span class="line">input m_x: <span class="number">2</span></span><br><span class="line">input m_y: <span class="number">2</span></span><br><span class="line">(<span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">(<span class="number">2</span>, <span class="number">2</span>)</span><br><span class="line">--------</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="23-2-调用父类的运算符重载"><a href="#23-2-调用父类的运算符重载" class="headerlink" title="23.2 调用父类的运算符重载"></a>23.2 调用父类的运算符重载</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line">    Person &amp;<span class="keyword">operator</span>=(<span class="keyword">const</span> Person &amp;person)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;m_age = person.m_age;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>: </span><br><span class="line">    <span class="keyword">int</span> m_score;</span><br><span class="line"></span><br><span class="line">    Student &amp;<span class="keyword">operator</span>=(<span class="keyword">const</span> Student &amp;student)</span><br><span class="line">    &#123;</span><br><span class="line">        Person::<span class="keyword">operator</span>=(student);</span><br><span class="line">        <span class="keyword">this</span>-&gt;m_score = student.m_score;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">showInfo</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; m_age &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; m_score &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Student s1;</span><br><span class="line">    s1.m_age = <span class="number">10</span>;</span><br><span class="line">    s1.m_score = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">    Student s2 = s1;</span><br><span class="line">    s2.showInfo();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\demo9&gt; .\demo5</span><br><span class="line"><span class="number">10</span> <span class="number">20</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="23-3-仿函数"><a href="#23-3-仿函数" class="headerlink" title="23.3 仿函数"></a>23.3 仿函数</h4><ul>
<li>定义：<ul>
<li>仿函数(<code>functor</code>)，就是使一个类的使用看上去像一个函数。其实现就是类中实现一个<code>operator</code>这个类就有了类似函数的行为</li>
<li>将一个对象，作为一个函数来使用</li>
<li>对比普通的函数，仿函数可以保存状态</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Sum</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="keyword">int</span> a = <span class="number">0</span>, <span class="keyword">int</span> b = <span class="number">0</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a + b;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sum s1;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; s1(<span class="number">10</span>, <span class="number">20</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印示例：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo10&gt; .\demo1</span><br><span class="line"><span class="number">30</span></span><br></pre></td></tr></table></figure>








</li>
</ul>
<h3 id="24-模板"><a href="#24-模板" class="headerlink" title="24. 模板"></a>24. 模板</h3><ul>
<li>泛型：一种类型参数化以达到代码复用的技术</li>
<li>种类：<code>函数模板</code>，<code>类模板</code></li>
<li>模板没有被使用，不会被实例化</li>
</ul>
<h4 id="24-1-函数模板定义"><a href="#24-1-函数模板定义" class="headerlink" title="24.1 函数模板定义"></a>24.1 函数模板定义</h4><ul>
<li><p>代码示例：<code>函数模板</code>, <code>多参数函数模板</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point);</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;</span><br><span class="line"></span><br><span class="line">    Point <span class="keyword">operator</span>+(<span class="keyword">const</span> Point &amp;point)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> Point(<span class="keyword">this</span>-&gt;m_x + point.m_x, <span class="keyword">this</span>-&gt;m_y + point.m_y);</span><br><span class="line">    &#125;         </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;(&quot;</span> &lt;&lt; point.m_x &lt;&lt; <span class="string">&quot;, &quot;</span> &lt;&lt; point.m_y &lt;&lt; <span class="string">&quot;)&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数模板</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function">T <span class="title">add</span><span class="params">(T a, T b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 多参数函数模板</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T_int, <span class="keyword">typename</span> T_double&gt;</span><br><span class="line"><span class="function">T_double <span class="title">add</span><span class="params">(T_int a, T_double b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; add&lt;<span class="keyword">int</span>&gt;(<span class="number">1</span>, <span class="number">2</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; add&lt;<span class="keyword">int</span>, <span class="keyword">double</span>&gt;(<span class="number">1</span>, <span class="number">2.1</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; add&lt;Point&gt;(Point(<span class="number">1</span>, <span class="number">1</span>), Point(<span class="number">2</span>, <span class="number">2</span>)) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo10&gt; .\demo1</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">3.1</span></span><br><span class="line">(<span class="number">3</span>, <span class="number">3</span>)</span><br></pre></td></tr></table></figure>







</li>
</ul>
<h4 id="24-2-函数模板分离"><a href="#24-2-函数模板分离" class="headerlink" title="24.2 函数模板分离"></a>24.2 函数模板分离</h4><ul>
<li>编译基本过程：<ul>
<li>先把<code>A.cpp</code>包含的<code>A.h</code>的内容复制到<code>A.cpp</code>中<code>(声明和实现回到同一个cpp文件中)</code></li>
<li>再编译<code>main.cpp</code>和<code>A.cpp</code>成<code>main.obj</code>和<code>A.obj</code></li>
<li>再通过<code>main.ilk</code>链接<code>main.cpp</code>和<code>A.cpp</code></li>
<li>修正了<code>main.cpp</code>中调用<code>A.cpp</code>中函数时，<code>call的函数地址</code></li>
<li>最后生成<code>main.exe</code></li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：<code>模板的错误编译</code></p>
</li>
<li><p><code>add.h</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function">T <span class="title">add</span><span class="params">(T a, T b)</span></span>;</span><br></pre></td></tr></table></figure></li>
<li><p><code>add.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#include &#x27;add.h&#x27;</span><br><span class="line"></span><br><span class="line"><span class="function">T <span class="title">add</span><span class="params">(T a, T b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p><code>main.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line">#include &#x27;add.h&#x27;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    add(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>以上代码编译不会报错，但<code>链接</code>报错，不会生成<code>.exe</code></p>
</li>
<li><p>原因：</p>
<ul>
<li><code>模板没有被使用，不会被实例化</code></li>
<li>所以<code>add.cpp</code>在编译过程中，编译器未发现在<code>add.cpp</code>中有使用<code>add()</code>模板函数的地方</li>
<li>于是，便将<code>add.h</code>和<code>add.cpp</code>中未被使用的<code>add()</code>模板函数优化了<code>(即便链接后，add()在main.cpp中被使用)</code></li>
<li>链接后，<code>main.cpp</code>中调用<code>add()</code>函数，得不到有效的<code>函数实现地址(此时main.cpp中是有函数声明的，因为包含了add.h)</code>，所以会报错</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：<code>正确的写法</code></p>
</li>
<li><p><code>add.h</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function">T <span class="title">add</span><span class="params">(T a, T b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p><code>main.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line">#include &#x27;add.h&#x27;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    add(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>写模板函数时，不要进行声明实现分离，全部写在<code>.h</code>中，正规的方式是<code>.hpp</code></p>
</li>
</ul>
<h4 id="24-3-类模板的定义"><a href="#24-3-类模板的定义" class="headerlink" title="24.3 类模板的定义"></a>24.3 类模板的定义</h4><ul>
<li>注意：类模板中的友元函数声明<ul>
<li>需先声明类和友元函数的模板</li>
<li>再实现友元函数</li>
<li>且最好友元函数的模板参数和类模板的参数类型不同</li>
<li>另一种方式：声明和实现中都要在<code>&lt;&lt;</code>后加入<code>&lt;&gt;</code><ul>
<li><code>ostream &amp;operator&lt;&lt; &lt;&gt;(ostream &amp;cout, const Array&lt;Arr&gt; &amp;arr);</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：</p>
</li>
<li><p><code>array.hpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Created by FHang on 2022/3/9 14:06</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> INC_8_CPP_STRUCTURE_SEQLIST_HPP</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> INC_8_CPP_STRUCTURE_SEQLIST_HPP</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="built_in">std</span>::<span class="built_in">cout</span>;</span><br><span class="line"><span class="keyword">using</span> <span class="built_in">std</span>::<span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXSIZE 100</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SeqList</span>;</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="built_in">std</span>::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(<span class="built_in">std</span>::ostream &amp;f_cout, <span class="keyword">const</span> SeqList&lt;T&gt; &amp;tab);</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SeqList</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    tab_T *m_tabData;</span><br><span class="line">    <span class="keyword">int</span> m_capacity;</span><br><span class="line">    <span class="keyword">int</span> m_size;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">SeqList</span><span class="params">(<span class="keyword">int</span> capacity)</span></span>;</span><br><span class="line">    ~SeqList();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">size</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line">    <span class="function">tab_T <span class="title">get</span><span class="params">(<span class="keyword">int</span> index)</span> <span class="keyword">const</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(tab_T value)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> i, tab_T value)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">remove</span><span class="params">(<span class="keyword">int</span> i)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">removeByRange</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">int</span> range)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">replaceByValue</span><span class="params">(tab_T tagValue, tab_T newValue)</span></span>;</span><br><span class="line"></span><br><span class="line">    tab_T <span class="keyword">operator</span>[](<span class="keyword">int</span> index) <span class="keyword">const</span>;</span><br><span class="line">    <span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line">    <span class="keyword">friend</span> <span class="built_in">std</span>::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(<span class="built_in">std</span>::ostream &amp;f_cout, <span class="keyword">const</span> SeqList&lt;T&gt; &amp;tab);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line">SeqList&lt;tab_T&gt;::SeqList(<span class="keyword">int</span> capacity)</span><br><span class="line">&#123;</span><br><span class="line">    m_capacity = (capacity &gt; <span class="number">0</span>) ? capacity : MAXSIZE;</span><br><span class="line">    m_tabData = <span class="keyword">new</span> tab_T[m_capacity];</span><br><span class="line">    <span class="keyword">if</span> (m_tabData == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="keyword">else</span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;&lt;-- SeqList Created --&gt;\n&quot;</span>;&#125;</span><br><span class="line">    m_size = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line">SeqList&lt;tab_T&gt;::~SeqList()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (m_tabData == <span class="literal">nullptr</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="keyword">else</span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;&lt;-- SeqList Deleted --&gt;\n&quot;</span>;&#125;</span><br><span class="line">    <span class="keyword">delete</span>[] m_tabData;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">int</span> SeqList&lt;tab_T&gt;::size() <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> m_size;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line">tab_T SeqList&lt;tab_T&gt;::get(<span class="keyword">int</span> index) <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (index &lt; <span class="number">0</span> || index &gt;= m_size)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">throw</span>(<span class="string">&quot;&lt;&lt; !!! 数组下标越界 !!! &gt;&gt;&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> m_tabData[index];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">void</span> SeqList&lt;tab_T&gt;::add(tab_T value)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (m_size &gt;= m_capacity)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//TODO Reset Capacity</span></span><br><span class="line">    &#125;</span><br><span class="line">    m_tabData[m_size] = value;</span><br><span class="line">    ++m_size;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">void</span> SeqList&lt;tab_T&gt;::insert(<span class="keyword">int</span> i, tab_T value)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> ((i &lt; <span class="number">0</span>) || (i &gt; m_size + <span class="number">1</span>))&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="keyword">if</span> (m_size == MAXSIZE)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = m_size - <span class="number">1</span>; j &gt;= i - <span class="number">1</span>; --j)</span><br><span class="line">    &#123;</span><br><span class="line">        m_tabData[j + <span class="number">1</span>] = m_tabData[j];</span><br><span class="line">    &#125;</span><br><span class="line">    m_tabData[i] = value;</span><br><span class="line">    ++m_size;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">void</span> SeqList&lt;tab_T&gt;::remove(<span class="keyword">int</span> i)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> ((i &lt; <span class="number">0</span>) || (i &gt; m_size + <span class="number">1</span>))&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &lt;= m_size <span class="number">-1</span>; ++j)</span><br><span class="line">    &#123;</span><br><span class="line">        m_tabData[j] = m_tabData[j + <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    --m_size;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">void</span> SeqList&lt;tab_T&gt;::removeByRange(<span class="keyword">int</span> i, <span class="keyword">int</span> range)</span><br><span class="line">&#123;</span><br><span class="line">     <span class="keyword">if</span> ((i &lt; <span class="number">0</span>) || (i &gt; m_size + <span class="number">1</span>))&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line"></span><br><span class="line">     <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &lt;= m_size - <span class="number">1</span>; ++j)</span><br><span class="line">     &#123;</span><br><span class="line">         m_tabData[j] = m_tabData[j + range];</span><br><span class="line">     &#125;</span><br><span class="line">     m_size -= range;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line"><span class="keyword">void</span> SeqList&lt;tab_T&gt;::replaceByValue(tab_T tagValue, tab_T newValue)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m_size; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (m_tabData[i] == tagValue)</span><br><span class="line">        &#123;</span><br><span class="line">            m_tabData[i] = newValue;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> tab_T&gt;</span><br><span class="line">tab_T SeqList&lt;tab_T&gt;::<span class="keyword">operator</span>[](<span class="keyword">int</span> index) <span class="keyword">const</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> get(index);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="built_in">std</span>::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(<span class="built_in">std</span>::ostream &amp;f_cout, <span class="keyword">const</span> SeqList&lt;T&gt; &amp;tab)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> size = tab.size();</span><br><span class="line">    f_cout &lt;&lt; <span class="string">&quot;[&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; size; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (i != <span class="number">0</span>)&#123;f_cout &lt;&lt; <span class="string">&quot;,&quot;</span>;&#125;</span><br><span class="line">        f_cout &lt;&lt; tab[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> f_cout &lt;&lt; <span class="string">&quot;]&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">//INC_8_CPP_STRUCTURE_SEQLIST_HPP</span></span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<p>  <code>Main.cpp</code></p>
  <figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Created by FHang on 2022/3/9 14:06</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;SeqList.hpp&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">SeqList&lt;<span class="keyword">int</span>&gt; <span class="title">seqList</span><span class="params">(<span class="number">10</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        seqList.add(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; seqList &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    seqList.insert(<span class="number">2</span>, <span class="number">9</span>);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; seqList &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    seqList.remove(<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; seqList &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    seqList.removeByRange(<span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; seqList &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    seqList.replaceByValue(<span class="number">1</span>, <span class="number">9</span>);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; seqList &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




<ul>
<li><p><code>demo2.cpp</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;array.hpp&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_x;</span><br><span class="line">    <span class="keyword">int</span> m_y;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Point(<span class="keyword">int</span> x = <span class="number">0</span>, <span class="keyword">int</span> y = <span class="number">0</span>) : m_x(x), m_y(y)&#123;&#125;      </span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">ostream &amp;<span class="keyword">operator</span>&lt;&lt;(ostream &amp;<span class="built_in">cout</span>, <span class="keyword">const</span> Point &amp;point)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;(&quot;</span> &lt;&lt; point.m_x &lt;&lt; <span class="string">&quot;, &quot;</span> &lt;&lt; point.m_y &lt;&lt; <span class="string">&quot;)&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;----Array&lt;int&gt;----&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="function">Array&lt;<span class="keyword">int</span>&gt; <span class="title">array</span><span class="params">(<span class="number">3</span>)</span></span>;</span><br><span class="line">    <span class="built_in">array</span>.add(<span class="number">1</span>);</span><br><span class="line">    <span class="built_in">array</span>.add(<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">array</span>.add(<span class="number">3</span>);</span><br><span class="line">    <span class="built_in">array</span>.add(<span class="number">4</span>);</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;arr[0]: &quot;</span> &lt;&lt; <span class="built_in">array</span>.get(<span class="number">0</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;arr[1]: &quot;</span> &lt;&lt; <span class="built_in">array</span>[<span class="number">1</span>] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;size: &quot;</span> &lt;&lt; <span class="built_in">array</span>.size() &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;arr: &quot;</span> &lt;&lt; <span class="built_in">array</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; &quot;\n&quot;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; &quot;----Array&lt;Point&gt;----<span class="string">&quot; &lt;&lt; endl;</span></span><br><span class="line"><span class="string">    Array&lt;Point&gt; arr_Point(2);</span></span><br><span class="line"><span class="string">    arr_Point.add(Point(1, 1));</span></span><br><span class="line"><span class="string">    arr_Point.add(Point(2, 2));</span></span><br><span class="line"><span class="string">    cout &lt;&lt; &quot;</span>arr_Point: <span class="string">&quot; &lt;&lt; arr_Point &lt;&lt; endl;</span></span><br><span class="line"><span class="string">    cout &lt;&lt; &quot;</span>\n<span class="string">&quot;;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    return 0;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo10&gt; .\demo2</span><br><span class="line">---<span class="literal">-Array</span>&lt;int&gt;----</span><br><span class="line">size not free</span><br><span class="line">arr[<span class="number">0</span>]: <span class="number">1</span></span><br><span class="line">arr[<span class="number">1</span>]: <span class="number">2</span></span><br><span class="line">size: <span class="number">3</span></span><br><span class="line">arr: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"></span><br><span class="line">---<span class="literal">-Array</span>&lt;Point&gt;----</span><br><span class="line">arr_Point: [(<span class="number">1</span>, <span class="number">1</span>), (<span class="number">2</span>, <span class="number">2</span>)]</span><br></pre></td></tr></table></figure>









</li>
</ul>
<h3 id="25-其它语法-特性"><a href="#25-其它语法-特性" class="headerlink" title="25. 其它语法-特性"></a>25. 其它语法-特性</h3><h4 id="25-1-类型转换"><a href="#25-1-类型转换" class="headerlink" title="25.1 类型转换"></a>25.1 类型转换</h4><ul>
<li><p>C语言风格的类型转换符</p>
<ul>
<li><code>(type)expression</code></li>
<li><code>type(expression)</code></li>
</ul>
</li>
<li><p>C++四种转换</p>
<ul>
<li><p><code>static_cast</code></p>
</li>
<li><p><code>dynamic_cast</code></p>
</li>
<li><p><code>reinterpret_cast</code></p>
</li>
<li><p><code>const_cast</code></p>
<p>使用格式：<code>xx_cast&lt;type&gt;(expression)</code></p>
</li>
</ul>
</li>
</ul>
<h5 id="25-1-1-const-cast"><a href="#25-1-1-const-cast" class="headerlink" title="25.1.1 const_cast"></a>25.1.1 const_cast</h5><ul>
<li>一般用于去除<code>const</code>属性，将<code>const</code>转换成<code>非const</code></li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>&#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">const</span> Person *p1 = <span class="keyword">new</span> Person;</span><br><span class="line">    <span class="comment">// 两种写法完全等价</span></span><br><span class="line">    Person *p2 = <span class="keyword">const_cast</span>&lt;Person *&gt;(p1);</span><br><span class="line">    Person *p3 = (Person *)p1;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Person *p2 &#x3D; const_cast&lt;Person *&gt;(p1);</span><br><span class="line">mov eax, dword ptr[p1]</span><br><span class="line">mov dword ptr[p2], eax</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; Person *p3 &#x3D; (Person *)p1;</span><br><span class="line">mov eax, dword ptr[p1]</span><br><span class="line">mov dword ptr[p3], eax</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h5 id="25-1-2-dynamic-cast"><a href="#25-1-2-dynamic-cast" class="headerlink" title="25.1.2 dynamic_cast"></a>25.1.2 dynamic_cast</h5><ul>
<li>一般用于多态类型转换，有运行时的安全检测</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> :</span> <span class="keyword">public</span> Person</span><br><span class="line">&#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person;</span><br><span class="line">    Person *p2 = <span class="keyword">new</span> Student;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 两种写法完全等价</span></span><br><span class="line">    Student *s1 = (Student *)p1; <span class="comment">// 不安全</span></span><br><span class="line">    Student *s1 = <span class="keyword">dynamic_cast</span>&lt;Student *&gt;(p1); <span class="comment">// 不安全</span></span><br><span class="line">    </span><br><span class="line">    Student *s2 = <span class="keyword">dynamic_cast</span>&lt;Student *&gt;(p2); <span class="comment">// 安全</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>反汇编查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Student *s1 &#x3D; dynamic_cast&lt;Student *&gt;(p1);</span><br><span class="line">mov eax, dword ptr[p1]</span><br><span class="line">call __RTDynamicCast(0C14ABh)</span><br><span class="line">mov dword ptr[s1], eax</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h5 id="25-1-3-static-cast"><a href="#25-1-3-static-cast" class="headerlink" title="25.1.3 static_cast"></a>25.1.3 static_cast</h5><ul>
<li>对比<code>dynamic_cast</code>确实安全检测</li>
<li>不能交叉转换（不是同一继承体系的，无法转换）</li>
<li>常用于基本数据类型转换，<code>非const</code>转换<code>const</code></li>
</ul>
<ul>
<li><p>代码示例：<code>交叉转换</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>&#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span>&#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 两个类之间完全没有关系，强行转化：交叉转换</span></span><br><span class="line">    Car *c1 = (Car *)p1;</span><br><span class="line">    Car *c2 = <span class="keyword">dynamic_cast</span>&lt;Car *&gt;(p1);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例：<code>非const</code>转换<code>const</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>&#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Person *p1 = <span class="keyword">new</span> Person;</span><br><span class="line">    <span class="keyword">const</span> Person *p2 = <span class="keyword">static_cast</span>&lt;Person *&gt;(p1);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h5 id="25-1-4-reinterpret-cast"><a href="#25-1-4-reinterpret-cast" class="headerlink" title="25.1.4 reinterpret_cast"></a>25.1.4 reinterpret_cast</h5><ul>
<li>属于比较底层的强制转换，没有任何类型检查和格式转换，仅仅只是简单的二进制数据拷贝</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// C语言的隐式转换</span></span><br><span class="line">    <span class="keyword">double</span> b = a;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">double</span> d = <span class="keyword">reinterpret_cast</span>&lt;<span class="keyword">double</span>&amp;&gt;(a);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>内存分析：反汇编</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">int a &#x3D; 10;</span><br><span class="line">&#x2F;&#x2F; 二进制：00001010 00000000 00000000 00000000</span><br><span class="line">&#x2F;&#x2F; 十六进制：0A 00 00 00</span><br><span class="line">&#x2F;&#x2F; 小段模式：int类型4字节，从右往左读取</span><br><span class="line"></span><br><span class="line">double b &#x3D; a;</span><br><span class="line">&#x2F;&#x2F; 00 00 00 00 00 00 24 40</span><br><span class="line">&#x2F;&#x2F; cvtsi2sd xmm0, dword ptr[a]</span><br><span class="line">&#x2F;&#x2F; movsd mmword ptr[d], xmm0</span><br><span class="line"></span><br><span class="line">double d &#x3D; reinterpret_cast&lt;double&amp;&gt;(a);</span><br><span class="line">&#x2F;&#x2F; 0A 00 00 00 cc cc cc cc</span><br><span class="line">&#x2F;&#x2F; movsd xmm0, mmword ptr[a]</span><br><span class="line">&#x2F;&#x2F; movsd mmword ptr[d], xmm0</span><br></pre></td></tr></table></figure>








</li>
</ul>
<h4 id="25-2-C-11特性"><a href="#25-2-C-11特性" class="headerlink" title="25.2 C++11特性"></a>25.2 C++11特性</h4><ul>
<li><p><code>auto</code>：</p>
<ul>
<li><p>可以初始化表达式中推断出变量的类型，提高开发效率</p>
</li>
<li><p>属于编译器特性，不影响最终的机器码质量，不影响运行效率</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> i = <span class="number">10</span>; <span class="comment">// int</span></span><br><span class="line"><span class="keyword">auto</span> str = <span class="string">&quot;c++&quot;</span>; <span class="comment">// const char *</span></span><br><span class="line"><span class="keyword">auto</span> p = <span class="keyword">new</span> Person; <span class="comment">// Person *</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
</li>
</ul>
<ul>
<li><p><code>decltype</code>:</p>
<ul>
<li><p>可以获取变量的类型</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">decltype</span>(a) b = <span class="number">20</span>;</span><br></pre></td></tr></table></figure>


</li>
</ul>
</li>
</ul>
<ul>
<li><p><code>nullptr</code>:</p>
<ul>
<li><p>可以解决<code>NULL</code>的二义性问题</p>
</li>
<li><p><code>NULL</code>本质是<code>define NULL 0</code></p>
</li>
<li><p>在<code>c++98</code>中<code>void fun(int)&#123;&#125;``void fun(int *)&#123;&#125;</code>时会遇到二义性，在<code>c++11</code>之后，默认为<code>0</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">(<span class="keyword">int</span> a)</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func(int): &quot;</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">(<span class="keyword">int</span> *a)</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func(int *): &quot;</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// func(NULL);</span></span><br><span class="line">    func(<span class="literal">nullptr</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo11&gt; .\demo3</span><br><span class="line">func(int *): <span class="number">0</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
</li>
</ul>
<ul>
<li><p><code>快速遍历</code>：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> arr[] = &#123;&#125;;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> a : arr)&#123;<span class="built_in">cout</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="25-3-Lambda表达式"><a href="#25-3-Lambda表达式" class="headerlink" title="25.3 Lambda表达式"></a>25.3 Lambda表达式</h4><ul>
<li>lambda表达式：<ul>
<li>本质是函数</li>
<li>完整结构：<code>[capture list] (params list) mutable exception -&gt; return type &#123;function body&#125;;</code><ul>
<li><code>capture list</code>：捕获外部变量列表</li>
<li><code>params list</code>：形参列表，不能使用默认参数，不能省略参数名</li>
<li><code>mutable</code>：用于说明是否可以修改捕获的变量</li>
<li><code>exception </code>：异常设定</li>
<li><code>return type</code>：返回值类型</li>
<li><code>function body</code>：函数体</li>
</ul>
</li>
<li>省略的写法：<ul>
<li><code>[capture list] (params list) -&gt; return type &#123;function body&#125;;</code></li>
<li><code>[capture list] (params list)&#123;function body&#125;; </code></li>
<li><code>[capture list]&#123;function body&#125;; </code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h5 id="25-3-1-lambda基本用法"><a href="#25-3-1-lambda基本用法" class="headerlink" title="25.3.1 lambda基本用法"></a>25.3.1 lambda基本用法</h5><ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 无参，无返回值，默认调用</span></span><br><span class="line">    ([]</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func() - 1&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;)();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可以用指针存储</span></span><br><span class="line">    <span class="keyword">void</span> (*p)() = []</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func() - p&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    p();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 直接用auto存储更方便</span></span><br><span class="line">    <span class="keyword">auto</span> p1 = []</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func() - p1&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    p1();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 有返回值和参数</span></span><br><span class="line">    <span class="keyword">auto</span> add1 = [](<span class="keyword">int</span> a, <span class="keyword">int</span> b) -&gt; <span class="keyword">auto</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> a + b;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; add1(<span class="number">1</span>, <span class="number">2</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回值类型也可省略</span></span><br><span class="line">    <span class="keyword">auto</span> add2 = [](<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> a + b;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; add2(<span class="number">2</span>, <span class="number">3</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo11&gt; .\demo4</span><br><span class="line">func() - <span class="number">1</span></span><br><span class="line">func() - p</span><br><span class="line">func() - p1</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">5</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">calculate</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> (*func)(<span class="keyword">int</span>, <span class="keyword">int</span>))</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; func(a, b) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    calculate(<span class="number">1</span>, <span class="number">2</span>, [](<span class="keyword">int</span> a, <span class="keyword">int</span> b)&#123;<span class="keyword">return</span> a + b;&#125;);</span><br><span class="line">    calculate(<span class="number">4</span>, <span class="number">2</span>, [](<span class="keyword">int</span> a, <span class="keyword">int</span> b)&#123;<span class="keyword">return</span> a - b;&#125;);</span><br><span class="line">    calculate(<span class="number">8</span>, <span class="number">2</span>, [](<span class="keyword">int</span> a, <span class="keyword">int</span> b)&#123;<span class="keyword">return</span> a * b;&#125;);</span><br><span class="line">    calculate(<span class="number">8</span>, <span class="number">2</span>, [](<span class="keyword">int</span> a, <span class="keyword">int</span> b)&#123;<span class="keyword">return</span> a / b;&#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo11&gt; .\demo5</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">16</span></span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
<h5 id="25-3-2-lambda变量捕获"><a href="#25-3-2-lambda变量捕获" class="headerlink" title="25.3.2 lambda变量捕获"></a>25.3.2 lambda变量捕获</h5><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span> b = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">auto</span> add1 = [a, b](<span class="keyword">int</span> c, <span class="keyword">int</span> d)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; a + b &lt;&lt; <span class="string">&quot;\n&quot;</span> &lt;&lt; c + d &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    add1(<span class="number">1</span>, <span class="number">98</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 隐式捕获:（全捕获）</span></span><br><span class="line">    <span class="comment">// [=] 值类型</span></span><br><span class="line">    <span class="comment">// [&amp;] 引用类型 </span></span><br><span class="line">    <span class="keyword">auto</span> add2 = [=]</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; a + b &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    add2();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="25-4-C-14特性"><a href="#25-4-C-14特性" class="headerlink" title="25.4 C++14特性"></a>25.4 C++14特性</h4><ul>
<li><p>泛型lambda表达式：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> func = [](<span class="keyword">auto</span> a, <span class="keyword">auto</span> b)&#123;<span class="keyword">return</span> a + b;&#125;;</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>捕获的变量进行初始化</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">9</span>;</span><br><span class="line"><span class="keyword">auto</span> func = [a = <span class="number">10</span>]&#123;<span class="built_in">cout</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;&#125;;</span><br><span class="line">func(); <span class="comment">//打印结果：10</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>; <span class="comment">// 打印结果：9</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="25-5-C-17特性"><a href="#25-5-C-17特性" class="headerlink" title="25.5 C++17特性"></a>25.5 C++17特性</h4><ul>
<li>可以进行初始化的<code>if</code>和<code>switch</code>语句</li>
</ul>
<ul>
<li><p>代码示例：<code>if</code>，<code>else if</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">int</span> a = <span class="number">10</span>; a &gt; <span class="number">0</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>代码示例：<code>switch</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(<span class="keyword">int</span> a = <span class="number">1</span>; a)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>








</li>
</ul>
<h3 id="26-异常"><a href="#26-异常" class="headerlink" title="26. 异常"></a>26. 异常</h3><h4 id="26-1-异常基本用法"><a href="#26-1-异常基本用法" class="headerlink" title="26.1 异常基本用法"></a>26.1 异常基本用法</h4><ul>
<li>编程过程中常见的错误：<ul>
<li>语法错误</li>
<li>逻辑错误</li>
<li>异常</li>
</ul>
</li>
<li>异常：一种程序运行过程中可能会发生的错误<ul>
<li>每次异常可能类型不同，不能确定</li>
<li>异常没有处理，程序会直接中断或崩溃等等</li>
</ul>
</li>
<li>异常处理过程：<ul>
<li><code>throw</code>异常后，会在当前函数中查找匹配的<code>catch</code></li>
<li>找不到就会终止当前的函数代码，去上一层函数中查找</li>
<li>如果都找不到，程序直接终止</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例1：简单的异常演示</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (i ; i &lt; <span class="number">99999</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">try</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> *p = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">999999</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span>(...)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;for loop &quot;</span> &lt;&lt; i &lt;&lt; <span class="string">&quot; error!&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：多次打印（每次的异常结果可能不同）</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo1</span><br><span class="line"><span class="keyword">for</span> loop <span class="number">6176</span> error!</span><br><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo1</span><br><span class="line"><span class="keyword">for</span> loop <span class="number">6180</span> error!</span><br><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo1</span><br><span class="line"><span class="keyword">for</span> loop <span class="number">6182</span> error!</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：主动抛出异常</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">divide</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; a / b &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        divide(<span class="number">10</span>, <span class="number">1</span>);</span><br><span class="line">        divide(<span class="number">10</span>, <span class="number">0</span>);</span><br><span class="line">        divide(<span class="number">10</span>, <span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(<span class="keyword">const</span> <span class="keyword">int</span> exc)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Divide Error &quot;</span> &lt;&lt; exc &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo2</span><br><span class="line"><span class="number">10</span></span><br><span class="line">Divide Error <span class="number">0</span></span><br></pre></td></tr></table></figure></li>
<li><p>总结：<code>throw</code>后面的类型，要和<code>catch</code>里面接受的类型<code>保持一致</code></p>
</li>
</ul>
<ul>
<li><p>代码示例3：异常处理过程</p>
</li>
<li><p>没有<code>catch</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func1</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func1()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">throw</span> <span class="number">0</span>; <span class="comment">// 抛出异常，func1()中没有catch，向上层函数func2()中找</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func1()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func2</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func2()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    func1(); <span class="comment">// throw 来到func2()，也没有发现catch，继续向上到main()</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func2()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;main()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    func2(); <span class="comment">// throw来到main()中，依然没有发现catch，此时程序终止</span></span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;main()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo3</span><br><span class="line">main()<span class="literal">-begin</span></span><br><span class="line">func2()<span class="literal">-begin</span></span><br><span class="line">func1()<span class="literal">-begin</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<ul>
<li><p>有<code>catch</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func1</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func1()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(<span class="keyword">int</span> exc)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Error &quot;</span> &lt;&lt; exc &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func1()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func2</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func2()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    func1();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;func2()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;main()-begin&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    func2();</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;main()-end&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo3</span><br><span class="line">main()<span class="literal">-begin</span></span><br><span class="line">func2()<span class="literal">-begin</span></span><br><span class="line">func1()<span class="literal">-begin</span></span><br><span class="line">Error <span class="number">0</span></span><br><span class="line">func1()<span class="literal">-end</span></span><br><span class="line">func2()<span class="literal">-end</span></span><br><span class="line">main()<span class="literal">-end</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="26-2-异常抛出声明"><a href="#26-2-异常抛出声明" class="headerlink" title="26.2 异常抛出声明"></a>26.2 异常抛出声明</h4><ul>
<li>为了增强可读性和方便团队协作，函数内部抛出异常，建议函数声明一下函数类型</li>
</ul>
<ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func1</span><span class="params">()</span> <span class="title">throw</span><span class="params">()</span></span>&#123;&#125; <span class="comment">// 不抛出异常</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func2</span><span class="params">()</span> <span class="title">throw</span><span class="params">(<span class="keyword">int</span>, <span class="keyword">double</span>)</span></span>&#123;&#125; <span class="comment">// 可能抛出 int，double 类型的异常</span></span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="26-3-自定义异常类型"><a href="#26-3-自定义异常类型" class="headerlink" title="26.3 自定义异常类型"></a>26.3 自定义异常类型</h4><ul>
<li><p>代码示例1：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Exception</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">what</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DivideException</span> :</span> <span class="keyword">public</span> Exception</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">what</span><span class="params">()</span></span>&#123;<span class="keyword">return</span> <span class="string">&quot;Divide Error&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">divide</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">0</span>)&#123;<span class="keyword">throw</span> DivideException();&#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; a / b &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        divide(<span class="number">10</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(DivideException e)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; e.what() &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo4</span><br><span class="line">Divide Error</span><br></pre></td></tr></table></figure>




</li>
</ul>
<ul>
<li><p>代码示例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Exception</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">what</span><span class="params">()</span> <span class="keyword">const</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DivideException</span> :</span> <span class="keyword">public</span> Exception</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:    </span><br><span class="line">    <span class="function"><span class="keyword">const</span> <span class="keyword">char</span> *<span class="title">what</span><span class="params">()</span> <span class="keyword">const</span></span>&#123;<span class="keyword">return</span> <span class="string">&quot;Divide Error&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">divide</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">0</span>)&#123;<span class="keyword">throw</span> DivideException();&#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; a / b &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        divide(<span class="number">10</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(<span class="keyword">const</span> Exception &amp;e)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; e.what() &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo12&gt; .\demo4</span><br><span class="line">Divide Error</span><br></pre></td></tr></table></figure>








</li>
</ul>
<h3 id="27-智能指针"><a href="#27-智能指针" class="headerlink" title="27. 智能指针"></a>27. 智能指针</h3><ul>
<li>目的：改善传统指针存在的问题</li>
</ul>
<ul>
<li>传统指针存在的问题：<ul>
<li>需要手动管理内存</li>
<li>容易发生内存泄漏<ul>
<li>忘记释放</li>
<li>出现抛出异常</li>
</ul>
</li>
<li>释放后会变成野指针</li>
</ul>
</li>
</ul>
<ul>
<li>智能指针的作用：<ul>
<li><code>auto_ptr</code>：C++98，存在缺陷，不能用于数组</li>
<li><code>shared_ptr</code>：C++11</li>
<li><code>unique_ptr</code>：C++11</li>
</ul>
</li>
</ul>
<h4 id="27-1-智能指针基本使用"><a href="#27-1-智能指针基本使用" class="headerlink" title="27.1 智能指针基本使用"></a>27.1 智能指针基本使用</h4><ul>
<li><p>代码示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;memory&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">int</span> m_age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Person(<span class="keyword">int</span> age) : m_age(age)&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person(int)&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">    ~Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()&quot;</span> &lt;&lt; <span class="built_in">endl</span>;&#125;        </span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::run() &quot;</span> &lt;&lt; m_age &lt;&lt; <span class="built_in">endl</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function"><span class="built_in">shared_ptr</span>&lt;Person&gt; <span class="title">p</span><span class="params">(<span class="keyword">new</span> Person(<span class="number">10</span>))</span></span>;</span><br><span class="line">    p-&gt;run();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    test1();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">  <span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo13&gt; .\demo1</span><br><span class="line">  Person::Person(int)</span><br><span class="line">  Person::run() <span class="number">10</span></span><br><span class="line">  Person::~Person()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 27.2 自定义智能指针</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">- 代码示例：</span><br><span class="line"></span><br><span class="line">  ```c++</span><br><span class="line">  <span class="comment">#include &lt;iostream&gt;</span></span><br><span class="line">  <span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line">  </span><br><span class="line">  template&lt;typename class_T&gt;</span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">fh_ptr</span></span></span><br><span class="line"><span class="class"></span>  &#123;</span><br><span class="line">  private:</span><br><span class="line">      class_T *obj_ptr;</span><br><span class="line">  </span><br><span class="line">  public:</span><br><span class="line">      fh_ptr(class_T *ptr) : obj_ptr(ptr)&#123;&#125;</span><br><span class="line">      ~fh_ptr()</span><br><span class="line">      &#123;</span><br><span class="line">          <span class="keyword">if</span> (obj_ptr == nullptr)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">          delete obj_ptr;</span><br><span class="line">          obj_ptr = nullptr;</span><br><span class="line">      &#125;    </span><br><span class="line">  </span><br><span class="line">      class_T *operator-&gt;()&#123;<span class="keyword">return</span> obj_ptr;&#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>  &#123;</span><br><span class="line">  private:</span><br><span class="line">      int m_age;</span><br><span class="line">  </span><br><span class="line">  public:</span><br><span class="line">      Person(int age) : m_age(age)&#123;cout &lt;&lt; <span class="string">&quot;Person::Person(int)&quot;</span> &lt;&lt; endl;&#125;</span><br><span class="line">      ~Person()&#123;cout &lt;&lt; <span class="string">&quot;Person::~Person()&quot;</span> &lt;&lt; endl;&#125;        </span><br><span class="line">  </span><br><span class="line">      void run()&#123;cout &lt;&lt; <span class="string">&quot;Person::run() &quot;</span> &lt;&lt; m_age &lt;&lt; endl;&#125;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  void test1()</span><br><span class="line">  &#123;</span><br><span class="line">      fh_ptr&lt;Person&gt; p(new Person(<span class="number">10</span>));</span><br><span class="line">      p-&gt;run();</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  int main()</span><br><span class="line">  &#123;</span><br><span class="line">      test1();</span><br><span class="line">      </span><br><span class="line">      <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo13&gt; .\demo2</span><br><span class="line">Person::Person(int)</span><br><span class="line">Person::run() <span class="number">10</span></span><br><span class="line">Person::~Person()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h4 id="27-3-shared-ptr"><a href="#27-3-shared-ptr" class="headerlink" title="27.3 shared_ptr"></a>27.3 shared_ptr</h4><h5 id="27-3-1-shared-ptr原理"><a href="#27-3-1-shared-ptr原理" class="headerlink" title="27.3.1 shared_ptr原理"></a>27.3.1 shared_ptr原理</h5><ul>
<li>一个<code>shared_ptr</code>会对一个对象产生强引用</li>
<li>每个对象都会有一个对应的强引用计数，记录着当前的对象被多少<code>shared_ptr</code>强引用<ul>
<li>可以通过<code>shared_ptr</code>的<code>use_count</code>函数获得强引用计数</li>
</ul>
</li>
<li>当有一个新的<code>shared_ptr</code>指向对象时，对象的强引用计数会<code>加1</code></li>
<li>当一个<code>shared_ptr</code>销毁时，对象的强引用计数<code>减1</code></li>
<li>当一个对象的强引用计数<code>为0</code>时，对象就会自动销毁</li>
</ul>
<h5 id="27-3-2-shared-ptr循环引用"><a href="#27-3-2-shared-ptr循环引用" class="headerlink" title="27.3.2 shared_ptr循环引用"></a>27.3.2 shared_ptr循环引用</h5><h6 id="27-3-2-1-演示循环引用问题"><a href="#27-3-2-1-演示循环引用问题" class="headerlink" title="27.3.2.1 演示循环引用问题"></a>27.3.2.1 演示循环引用问题</h6><ul>
<li><p>循环引用的问题：</p>
<ul>
<li><p><code>shared_ptr</code>是<code>强引用</code>智能指针</p>
</li>
<li><p>发生两个类的对象相互引用，产生循环引用</p>
</li>
<li><p>循环引用会产生栈空间的智能指针对象销毁后，指向堆区的对象不会被销毁</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：<code>演示循环引用的问题</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;memory&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">shared_ptr</span>&lt;Person&gt; m_person;</span><br><span class="line">    Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()\n&quot;</span>;&#125;</span><br><span class="line">    ~Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::~Car()\n&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">shared_ptr</span>&lt;Car&gt; m_car;</span><br><span class="line">    Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()\n&quot;</span>;&#125;</span><br><span class="line">    ~Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()\n&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function"><span class="built_in">shared_ptr</span>&lt;Person&gt; <span class="title">person</span><span class="params">(<span class="keyword">new</span> Person)</span></span>;</span><br><span class="line">    <span class="function"><span class="built_in">shared_ptr</span>&lt;Car&gt; <span class="title">car</span><span class="params">(<span class="keyword">new</span> Car)</span></span>;</span><br><span class="line"></span><br><span class="line">    person-&gt;m_car = car;</span><br><span class="line">    car-&gt;m_person = person;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    test();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo13&gt; .\demo3</span><br><span class="line">Person::Person()</span><br><span class="line">Car::Car()</span><br></pre></td></tr></table></figure>




</li>
</ul>
<h6 id="27-3-2-2-解决循环引用问题"><a href="#27-3-2-2-解决循环引用问题" class="headerlink" title="27.3.2.2 解决循环引用问题"></a>27.3.2.2 解决循环引用问题</h6><ul>
<li><code>weak_ptr</code><ul>
<li>会对对象产生<code>弱引用</code></li>
<li>解决<code>shared_ptr</code>的循环引用问题</li>
</ul>
</li>
</ul>
<ul>
<li><p>代码示例：<code>解决循环引用问题</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;memory&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 只需要其中一个 是 弱引用 即可</span></span><br><span class="line">    weak_ptr&lt;Person&gt; m_person;</span><br><span class="line">    Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::Car()\n&quot;</span>;&#125;</span><br><span class="line">    ~Car()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Car::~Car()\n&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">shared_ptr</span>&lt;Car&gt; m_car;</span><br><span class="line">    Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::Person()\n&quot;</span>;&#125;</span><br><span class="line">    ~Person()&#123;<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Person::~Person()\n&quot;</span>;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function"><span class="built_in">shared_ptr</span>&lt;Person&gt; <span class="title">person</span><span class="params">(<span class="keyword">new</span> Person)</span></span>;</span><br><span class="line">    <span class="function"><span class="built_in">shared_ptr</span>&lt;Car&gt; <span class="title">car</span><span class="params">(<span class="keyword">new</span> Car)</span></span>;</span><br><span class="line"></span><br><span class="line">    person-&gt;m_car = car;</span><br><span class="line">    car-&gt;m_person = person;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    test();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>打印结果：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\Admin\Desktop\Project\Demo13&gt; .\demo3</span><br><span class="line">Person::Person()</span><br><span class="line">Car::Car()</span><br><span class="line">Person::~Person()</span><br><span class="line">Car::~Car()</span><br></pre></td></tr></table></figure>






</li>
</ul>
<h4 id="27-4-unique-ptr"><a href="#27-4-unique-ptr" class="headerlink" title="27.4 unique_ptr"></a>27.4 unique_ptr</h4><ul>
<li><p>同样是<code>强引用</code>，但可以确保同一时间只有<code>一个指针</code>指向对象</p>
</li>
<li><p>当<code>unique</code>销毁时，指向的对象也就自动销毁</p>
</li>
<li><p>使用<code>std::move</code>函数转移<code>unqiue_ptr</code>的所有权</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">unique_ptr</span>&lt;A&gt; a1;</span><br><span class="line"><span class="function"><span class="built_in">unique_ptr</span>&lt;A&gt; <span class="title">a2</span><span class="params">(<span class="keyword">new</span> A)</span></span>;</span><br><span class="line">a1 = <span class="built_in">std</span>::move(a2)</span><br></pre></td></tr></table></figure>

</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">FHang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://fhangh.gitee.io/article/c8cc223c.html">https://fhangh.gitee.io/article/c8cc223c.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://fhangh.gitee.io" target="_blank">Element</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/fhangh/tags/Assemly/">Assemly</a><a class="post-meta__tags" href="/fhangh/tags/C-C/">C/C++</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_22.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/fhangh/article/6150e7ca.html"><img class="prev-cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_23.jpg" onerror="onerror=null;src='/fhangh/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">汇编_辅助C++</div></div></a></div><div class="next-post pull-right"><a href="/fhangh/article/94096bb7.html"><img class="next-cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_21.jpg" onerror="onerror=null;src='/fhangh/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">C++_STL算法</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/fhangh/article/6150e7ca.html" title="汇编_辅助C++"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_23.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-12-05</div><div class="title">汇编_辅助C++</div></div></a></div><div><a href="/fhangh/article/94096bb7.html" title="C++_STL算法"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_21.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-21</div><div class="title">C++_STL算法</div></div></a></div><div><a href="/fhangh/article/1b8e5453.html" title="C++_STL函数对象"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_20.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-14</div><div class="title">C++_STL函数对象</div></div></a></div><div><a href="/fhangh/article/1505386e.html" title="C++_模板和STL"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_18.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-19</div><div class="title">C++_模板和STL</div></div></a></div><div><a href="/fhangh/article/5dfd4070.html" title="C++_职工管理系统"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_17.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-30</div><div class="title">C++_职工管理系统</div></div></a></div><div><a href="/fhangh/article/9ec06885.html" title="C语言基础"><img class="cover" data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_11.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-02-17</div><div class="title">C语言基础</div></div></a></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="/fhangh/img/fh.png" onerror="this.onerror=null;this.src='/fhangh/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">FHang</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/fhangh/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div><div class="card-info-data-item is-center"><a href="/fhangh/tags/"><div class="headline">标签</div><div class="length-num">10</div></a></div><div class="card-info-data-item is-center"><a href="/fhangh/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FHangH"><i class="fab fa-github"></i><span>Link To</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/FHangH" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:752972182@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">个人学习笔记</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#C-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%8F%8D%E6%B1%87%E7%BC%96%E8%A1%A5%E5%85%85"><span class="toc-number">1.</span> <span class="toc-text">C++_面向对象反汇编补充</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%B1%BB"><span class="toc-number">1.0.1.</span> <span class="toc-text">1. 类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-%E7%B1%BB%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.1.1.</span> <span class="toc-text">1.1 类的定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-struct%E5%92%8Cclass%E5%8C%BA%E5%88%AB"><span class="toc-number">1.0.1.2.</span> <span class="toc-text">1.2 struct和class区别</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-1-%E5%AE%9A%E4%B9%89%E5%92%8C%E8%AE%BF%E9%97%AE"><span class="toc-number">1.0.1.2.1.</span> <span class="toc-text">1.2.1 定义和访问</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-2-2-%E5%8F%8D%E6%B1%87%E7%BC%96%E6%9F%A5%E7%9C%8B%E5%8C%BA%E5%88%AB"><span class="toc-number">1.0.1.2.2.</span> <span class="toc-text">1.2.2 反汇编查看区别</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80"><span class="toc-number">1.0.1.3.</span> <span class="toc-text">1.3 对象的内存布局</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-this"><span class="toc-number">1.0.2.</span> <span class="toc-text">2. this</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%86%85%E5%AD%98%E7%A9%BA%E9%97%B4%E7%9A%84%E5%B8%83%E5%B1%80"><span class="toc-number">1.0.3.</span> <span class="toc-text">3. 内存空间的布局</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E4%BB%A3%E7%A0%81%E5%8C%BA"><span class="toc-number">1.0.3.1.</span> <span class="toc-text">3.1 代码区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E5%85%A8%E5%B1%80%E5%8C%BA"><span class="toc-number">1.0.3.2.</span> <span class="toc-text">3.2 全局区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-%E5%A0%86%E7%A9%BA%E9%97%B4"><span class="toc-number">1.0.3.3.</span> <span class="toc-text">3.3 堆空间</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#3-3-1-%E5%A0%86%E7%A9%BA%E9%97%B4%E7%94%B3%E8%AF%B7%E5%92%8C%E9%87%8A%E6%94%BE"><span class="toc-number">1.0.3.3.1.</span> <span class="toc-text">3.3.1 堆空间申请和释放</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-3-2-%E5%A0%86%E7%A9%BA%E9%97%B4%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.0.3.3.2.</span> <span class="toc-text">3.3.2 堆空间初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#3-3-2-1-memset"><span class="toc-number">1.0.3.3.2.1.</span> <span class="toc-text">3.3.2.1 memset</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%86%85%E5%AD%98"><span class="toc-number">1.0.3.4.</span> <span class="toc-text">3.4 对象的内存</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.4.</span> <span class="toc-text">4. 构造函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%9A%84%E9%94%99%E8%AF%AF%E6%A6%82%E5%BF%B5"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">4.1 构造函数的错误概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-number">1.0.4.2.</span> <span class="toc-text">4.2 构造函数的调用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.0.4.3.</span> <span class="toc-text">4.3 构造函数的成员变量初始化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%9A%84%E9%9B%86%E4%BD%93%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.0.4.4.</span> <span class="toc-text">4.4 构造函数的集体初始化</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.5.</span> <span class="toc-text">5. 析构函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-malloc%E4%B8%8E%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.5.1.</span> <span class="toc-text">5.1 malloc与析构函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-%E5%AF%B9%E8%B1%A1%E5%86%85%E7%94%B3%E8%AF%B7%E7%9A%84%E5%A0%86%E5%8C%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.0.5.2.</span> <span class="toc-text">5.2 对象内申请的堆区对象</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E5%A3%B0%E6%98%8E%E5%92%8C%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.0.6.</span> <span class="toc-text">6. 声明和实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-%E5%85%B7%E4%BD%93%E6%BC%94%E7%A4%BA"><span class="toc-number">1.0.6.1.</span> <span class="toc-text">6.1 具体演示</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4"><span class="toc-number">1.0.7.</span> <span class="toc-text">7. 命名空间</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-1-%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="toc-number">1.0.7.1.</span> <span class="toc-text">7.1 命名空间的基本用法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E7%9A%84%E5%B5%8C%E5%A5%97"><span class="toc-number">1.0.7.2.</span> <span class="toc-text">7.2 命名空间的嵌套</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-3-%E5%AD%98%E5%9C%A8%E6%9C%80%E5%A4%A7%E9%BB%98%E8%AE%A4%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4"><span class="toc-number">1.0.7.3.</span> <span class="toc-text">7.3 存在最大默认命名空间</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E7%BB%A7%E6%89%BF"><span class="toc-number">1.0.8.</span> <span class="toc-text">8. 继承</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1-%E7%BB%A7%E6%89%BF%E7%9A%84%E5%86%85%E5%AD%98%E5%B8%83%E5%B1%80"><span class="toc-number">1.0.8.1.</span> <span class="toc-text">8.1 继承的内存布局</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E6%88%90%E5%91%98%E8%AE%BF%E9%97%AE%E6%9D%83%E9%99%90"><span class="toc-number">1.0.8.2.</span> <span class="toc-text">8.2 成员访问权限</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8"><span class="toc-number">1.0.9.</span> <span class="toc-text">9. 初始化列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%BA%92%E8%B0%83"><span class="toc-number">1.0.10.</span> <span class="toc-text">10. 构造函数互调</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#10-1-%E4%BA%92%E8%B0%83%E8%AF%AD%E6%B3%95"><span class="toc-number">1.0.10.1.</span> <span class="toc-text">10.1 互调语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-2-%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90"><span class="toc-number">1.0.10.2.</span> <span class="toc-text">10.2 语法分析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-3-%E7%88%B6%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.10.3.</span> <span class="toc-text">10.3 父类的构造函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-4-%E6%9E%84%E9%80%A0%E5%92%8C%E6%9E%90%E6%9E%84%E7%9A%84%E9%A1%BA%E5%BA%8F"><span class="toc-number">1.0.10.4.</span> <span class="toc-text">10.4 构造和析构的顺序</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-%E5%A4%9A%E6%80%81"><span class="toc-number">1.0.11.</span> <span class="toc-text">11. 多态</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#11-1-%E7%88%B6%E7%B1%BB%E5%92%8C%E5%AD%90%E7%B1%BB%E6%8C%87%E9%92%88"><span class="toc-number">1.0.11.1.</span> <span class="toc-text">11.1 父类和子类指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-2-%E8%99%9A%E5%87%BD%E6%95%B0%E5%AE%9E%E7%8E%B0%E5%A4%9A%E6%80%81"><span class="toc-number">1.0.11.2.</span> <span class="toc-text">11.2 虚函数实现多态</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-3-%E8%99%9A%E8%A1%A8"><span class="toc-number">1.0.11.3.</span> <span class="toc-text">11.3 虚表</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#11-3-1-%E8%99%9A%E8%A1%A8%E7%9A%84%E5%A4%A7%E5%B0%8F"><span class="toc-number">1.0.11.3.1.</span> <span class="toc-text">11.3.1 虚表的大小</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#11-3-2-%E5%8F%8D%E6%B1%87%E7%BC%96%E5%88%86%E6%9E%90%E8%99%9A%E8%A1%A8"><span class="toc-number">1.0.11.3.2.</span> <span class="toc-text">11.3.2 反汇编分析虚表</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#11-3-3-%E8%99%9A%E8%A1%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%80%9D%E6%83%B3"><span class="toc-number">1.0.11.3.3.</span> <span class="toc-text">11.3.3 虚表的设计思想</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#11-3-4-%E5%A4%9A%E6%80%81%E5%AE%9E%E7%8E%B0%E7%9A%84%E9%9C%80%E6%B1%82"><span class="toc-number">1.0.11.3.4.</span> <span class="toc-text">11.3.4 多态实现的需求</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-4-%E8%99%9A%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.11.4.</span> <span class="toc-text">11.4 虚析构函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-5-%E7%BA%AF%E8%99%9A%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.11.5.</span> <span class="toc-text">11.5 纯虚函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-6-%E5%A4%9A%E7%BB%A7%E6%89%BF"><span class="toc-number">1.0.11.6.</span> <span class="toc-text">11.6 多继承</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#11-6-1-%E5%A4%9A%E7%BB%A7%E6%89%BF%E6%A6%82%E5%BF%B5"><span class="toc-number">1.0.11.6.1.</span> <span class="toc-text">11.6.1 多继承概念</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#11-6-2-%E5%90%8C%E5%90%8D%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0-%E5%8F%98%E9%87%8F"><span class="toc-number">1.0.11.6.2.</span> <span class="toc-text">11.6.2 同名成员函数&#x2F;变量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#11-6-3-%E8%8F%B1%E5%BD%A2%E7%BB%A7%E6%89%BF"><span class="toc-number">1.0.11.6.3.</span> <span class="toc-text">11.6.3 菱形继承</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#11-6-3-1-%E8%8F%B1%E5%BD%A2%E7%BB%A7%E6%89%BF%E7%BB%93%E6%9E%84"><span class="toc-number">1.0.11.6.3.1.</span> <span class="toc-text">11.6.3.1 菱形继承结构</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#11-6-3-2-%E8%99%9A%E7%BB%A7%E6%89%BF"><span class="toc-number">1.0.11.6.3.2.</span> <span class="toc-text">11.6.3.2 虚继承</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="toc-number">1.0.12.</span> <span class="toc-text">12. 静态成员</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#12-1-%E9%9D%99%E6%80%81%E6%88%90%E5%91%98%E5%9F%BA%E6%9C%AC%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.12.1.</span> <span class="toc-text">12.1 静态成员基本定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-2-%E5%8F%8D%E6%B1%87%E7%BC%96%E5%88%86%E6%9E%90%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="toc-number">1.0.12.2.</span> <span class="toc-text">12.2 反汇编分析静态成员</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#12-3-%E9%9D%99%E6%80%81%E6%88%90%E5%91%98%E5%BA%94%E7%94%A8"><span class="toc-number">1.0.12.3.</span> <span class="toc-text">12.3 静态成员应用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#12-3-1-%E7%BB%9F%E8%AE%A1%E5%AF%B9%E8%B1%A1%E4%B8%AA%E6%95%B0"><span class="toc-number">1.0.12.3.1.</span> <span class="toc-text">12.3.1 统计对象个数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#12-3-2-%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"><span class="toc-number">1.0.12.3.2.</span> <span class="toc-text">12.3.2 单例模式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#13-const%E6%88%90%E5%91%98"><span class="toc-number">1.0.13.</span> <span class="toc-text">13. const成员</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#14-%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B%E6%88%90%E5%91%98"><span class="toc-number">1.0.14.</span> <span class="toc-text">14. 引用类型成员</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#15-%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.15.</span> <span class="toc-text">15. 拷贝构造函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#15-1-%E9%BB%98%E8%AE%A4%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.15.1.</span> <span class="toc-text">15.1 默认拷贝构造函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-2-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.15.2.</span> <span class="toc-text">15.2 自定义拷贝构造函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-3-%E8%B0%83%E7%94%A8%E7%88%B6%E7%B1%BB%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.15.3.</span> <span class="toc-text">15.3 调用父类拷贝构造函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-4-%E6%B5%85%E5%A4%8D%E5%88%B6"><span class="toc-number">1.0.15.4.</span> <span class="toc-text">15.4 浅复制</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-5-%E6%B5%85-%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">1.0.15.5.</span> <span class="toc-text">15.5 浅&#x2F;深拷贝</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#15-5-1-%E8%A7%A3%E5%86%B3%E6%B5%85%E6%8B%B7%E8%B4%9D%E9%97%AE%E9%A2%98"><span class="toc-number">1.0.15.5.1.</span> <span class="toc-text">15.5.1 解决浅拷贝问题</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#15-5-2-%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">1.0.15.5.2.</span> <span class="toc-text">15.5.2 深拷贝</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#16-%E5%AF%B9%E8%B1%A1%E7%B1%BB%E5%9E%8B%E5%8F%82%E6%95%B0%E5%92%8C%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">1.0.16.</span> <span class="toc-text">16. 对象类型参数和返回值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#17-%E5%8C%BF%E5%90%8D%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.0.17.</span> <span class="toc-text">17. 匿名对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#18-%E9%9A%90%E5%BC%8F%E6%9E%84%E9%80%A0"><span class="toc-number">1.0.18.</span> <span class="toc-text">18. 隐式构造</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#19-%E7%BC%96%E8%AF%91%E5%99%A8%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.19.</span> <span class="toc-text">19. 编译器自动生成的构造函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#20-%E5%8F%8B%E5%85%83"><span class="toc-number">1.0.20.</span> <span class="toc-text">20. 友元</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#21-%E5%86%85%E9%83%A8%E7%B1%BB"><span class="toc-number">1.0.21.</span> <span class="toc-text">21. 内部类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#21-1-%E5%86%85%E9%83%A8%E7%B1%BB%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.21.1.</span> <span class="toc-text">21.1 内部类的定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#21-2-%E5%86%85%E9%83%A8%E7%B1%BB%E5%A3%B0%E6%98%8E%E4%B8%8E%E5%AE%9E%E7%8E%B0%E5%88%86%E7%A6%BB"><span class="toc-number">1.0.21.2.</span> <span class="toc-text">21.2 内部类声明与实现分离</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#22-%E5%B1%80%E9%83%A8%E7%B1%BB"><span class="toc-number">1.0.22.</span> <span class="toc-text">22. 局部类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#23-%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-number">1.0.23.</span> <span class="toc-text">23. 运算符重载</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#23-1-%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-number">1.0.23.1.</span> <span class="toc-text">23.1 基本运算符重载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#23-2-%E8%B0%83%E7%94%A8%E7%88%B6%E7%B1%BB%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-number">1.0.23.2.</span> <span class="toc-text">23.2 调用父类的运算符重载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#23-3-%E4%BB%BF%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.23.3.</span> <span class="toc-text">23.3 仿函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#24-%E6%A8%A1%E6%9D%BF"><span class="toc-number">1.0.24.</span> <span class="toc-text">24. 模板</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#24-1-%E5%87%BD%E6%95%B0%E6%A8%A1%E6%9D%BF%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.24.1.</span> <span class="toc-text">24.1 函数模板定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#24-2-%E5%87%BD%E6%95%B0%E6%A8%A1%E6%9D%BF%E5%88%86%E7%A6%BB"><span class="toc-number">1.0.24.2.</span> <span class="toc-text">24.2 函数模板分离</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#24-3-%E7%B1%BB%E6%A8%A1%E6%9D%BF%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.24.3.</span> <span class="toc-text">24.3 类模板的定义</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#25-%E5%85%B6%E5%AE%83%E8%AF%AD%E6%B3%95-%E7%89%B9%E6%80%A7"><span class="toc-number">1.0.25.</span> <span class="toc-text">25. 其它语法-特性</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#25-1-%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.0.25.1.</span> <span class="toc-text">25.1 类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#25-1-1-const-cast"><span class="toc-number">1.0.25.1.1.</span> <span class="toc-text">25.1.1 const_cast</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#25-1-2-dynamic-cast"><span class="toc-number">1.0.25.1.2.</span> <span class="toc-text">25.1.2 dynamic_cast</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#25-1-3-static-cast"><span class="toc-number">1.0.25.1.3.</span> <span class="toc-text">25.1.3 static_cast</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#25-1-4-reinterpret-cast"><span class="toc-number">1.0.25.1.4.</span> <span class="toc-text">25.1.4 reinterpret_cast</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#25-2-C-11%E7%89%B9%E6%80%A7"><span class="toc-number">1.0.25.2.</span> <span class="toc-text">25.2 C++11特性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#25-3-Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.0.25.3.</span> <span class="toc-text">25.3 Lambda表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#25-3-1-lambda%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="toc-number">1.0.25.3.1.</span> <span class="toc-text">25.3.1 lambda基本用法</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#25-3-2-lambda%E5%8F%98%E9%87%8F%E6%8D%95%E8%8E%B7"><span class="toc-number">1.0.25.3.2.</span> <span class="toc-text">25.3.2 lambda变量捕获</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#25-4-C-14%E7%89%B9%E6%80%A7"><span class="toc-number">1.0.25.4.</span> <span class="toc-text">25.4 C++14特性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#25-5-C-17%E7%89%B9%E6%80%A7"><span class="toc-number">1.0.25.5.</span> <span class="toc-text">25.5 C++17特性</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#26-%E5%BC%82%E5%B8%B8"><span class="toc-number">1.0.26.</span> <span class="toc-text">26. 异常</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#26-1-%E5%BC%82%E5%B8%B8%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="toc-number">1.0.26.1.</span> <span class="toc-text">26.1 异常基本用法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#26-2-%E5%BC%82%E5%B8%B8%E6%8A%9B%E5%87%BA%E5%A3%B0%E6%98%8E"><span class="toc-number">1.0.26.2.</span> <span class="toc-text">26.2 异常抛出声明</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#26-3-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.0.26.3.</span> <span class="toc-text">26.3 自定义异常类型</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#27-%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88"><span class="toc-number">1.0.27.</span> <span class="toc-text">27. 智能指针</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#27-1-%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-number">1.0.27.1.</span> <span class="toc-text">27.1 智能指针基本使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#27-3-shared-ptr"><span class="toc-number">1.0.27.2.</span> <span class="toc-text">27.3 shared_ptr</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#27-3-1-shared-ptr%E5%8E%9F%E7%90%86"><span class="toc-number">1.0.27.2.1.</span> <span class="toc-text">27.3.1 shared_ptr原理</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#27-3-2-shared-ptr%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8"><span class="toc-number">1.0.27.2.2.</span> <span class="toc-text">27.3.2 shared_ptr循环引用</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#27-3-2-1-%E6%BC%94%E7%A4%BA%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-number">1.0.27.2.2.1.</span> <span class="toc-text">27.3.2.1 演示循环引用问题</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#27-3-2-2-%E8%A7%A3%E5%86%B3%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-number">1.0.27.2.2.2.</span> <span class="toc-text">27.3.2.2 解决循环引用问题</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#27-4-unique-ptr"><span class="toc-number">1.0.27.3.</span> <span class="toc-text">27.4 unique_ptr</span></a></li></ol></li></ol></li></ol></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/fhangh/article/3fadb38f.html" title="UE4联网和多人游戏"><img data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_27.jpg" onerror="this.onerror=null;this.src='/fhangh/img/404.jpg'" alt="UE4联网和多人游戏"/></a><div class="content"><a class="title" href="/fhangh/article/3fadb38f.html" title="UE4联网和多人游戏">UE4联网和多人游戏</a><time datetime="2022-05-26T14:04:45.432Z" title="发表于 2022-05-26 22:04:45">2022-05-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/fhangh/article/7dd18469.html" title="UE4 RPC"><img data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_26.jpg" onerror="this.onerror=null;this.src='/fhangh/img/404.jpg'" alt="UE4 RPC"/></a><div class="content"><a class="title" href="/fhangh/article/7dd18469.html" title="UE4 RPC">UE4 RPC</a><time datetime="2022-05-19T04:30:00.826Z" title="发表于 2022-05-19 12:30:00">2022-05-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/fhangh/article/cb9854b2.html" title="UE4 MySQL插件使用"><img data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_25.jpg" onerror="this.onerror=null;this.src='/fhangh/img/404.jpg'" alt="UE4 MySQL插件使用"/></a><div class="content"><a class="title" href="/fhangh/article/cb9854b2.html" title="UE4 MySQL插件使用">UE4 MySQL插件使用</a><time datetime="2022-03-27T06:49:30.840Z" title="发表于 2022-03-27 14:49:30">2022-03-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/fhangh/article/605bd1ac.html" title="UE4 MySQL插件开发"><img data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_24.jpg" onerror="this.onerror=null;this.src='/fhangh/img/404.jpg'" alt="UE4 MySQL插件开发"/></a><div class="content"><a class="title" href="/fhangh/article/605bd1ac.html" title="UE4 MySQL插件开发">UE4 MySQL插件开发</a><time datetime="2022-03-26T07:56:25.807Z" title="发表于 2022-03-26 15:56:25">2022-03-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/fhangh/article/f7ede91d.html" title="Java基础"><img data-lazy-src="https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img_mini/postImg_19.jpg" onerror="this.onerror=null;this.src='/fhangh/img/404.jpg'" alt="Java基础"/></a><div class="content"><a class="title" href="/fhangh/article/f7ede91d.html" title="Java基础">Java基础</a><time datetime="2021-12-12T07:47:44.788Z" title="发表于 2021-12-12 15:47:44">2021-12-12</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(https://cdn.jsdelivr.net/gh/FHangH/FHangBlogCDN/Post_Img/postImg_22.jpg)"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By FHang</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/fhangh/js/utils.js"></script><script src="/fhangh/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/fhangh/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})</script><div class="js-pjax"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script></div></body></html>